如何编写 VBA 外壳脚本以在变量文件上运行 PHP 脚本



我正在尝试编写一个 shell 脚本,该脚本将同时在多个文件上运行 PHP 脚本(这需要在 json 文件上使用 exe 文件和 php 文件(。在这种情况下,json 文件是唯一

可变的文件。如果我手动使用命令提示符,这是我需要输入的命令才能获得所需的结果-

C:/Users/myusername/Desktop/XAMPP/php/php.exe transcript.php transcribedfile.json

我的工作表的单元格 G2 包含 json 文件名,因此这就是我在第二行激活它的原因。一旦我可以让 shell 脚本工作,我将在一个循环中编码,该循环将循环遍历 G 列中的所有值,从 G2 开始,直到 ActiveCell 值为 "。

这是我尝试过的——

Sub Shell()
Range("G2").Activate
Dim strProgramName As String
Dim strArgument As String
strProgramName = "C:UsersmyusernameDesktopXAMPPphpphp.exe transcript.php " & ActiveCell.Value
strArgument = "/G"
Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
End Sub

当我运行sub时,我收到"找不到文件"错误。我测试了strProgramName,发现如果我在那个时候切断代码,我可以让php.exe运行,但我需要它来识别整个字符串。

欢迎任何想法。提前谢谢你。

以下是我的做法:

Const PHP_PATH As String = "C:UsersmyusernameDesktopXAMPPphp"
Dim strProg As Variant
strProg = """{pth}php.exe"" ""{pth}transcript.php"" ""{pth}" & _
Range("G2").Value & """ G"
strProg = Replace(strProg, "{pth}", PHP_PATH)
Debug.Print strProg '<< pass this to Shell

最新更新