如何在变量文件上编写 VBA Shell 脚本到 运行 PHP 脚本

How to Write a VBA Shell Script to Run a PHP Script on Variable Files

我正在尝试编写一个 shell 脚本,它将 运行 一个 PHP 脚本同时用于多个文件(这需要使用一个 exe 文件和一个 php 文件放在 json 文件上)。 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:\Users\myusername\Desktop\XAMPP\php\php.exe transcript.php " & ActiveCell.Value
strArgument = "/G"

Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)

End Sub

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

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

我会这样做:

Const PHP_PATH As String = "C:\Users\myusername\Desktop\XAMPP\php\"
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