使用 VBA WScript.Shell 参数调用 .com 文件
Call .com file with parameters using VBA WScript.Shell
我正在使用 Excel 将一些文件上传到带有 WinSCP 的服务器上。
这个例子有效:
Sub FTP_upload()
Dim logfile, ftp_login, file_to_upload, upload_to_folder As String
logfile = "D:\temp\ftp.log"
ftp_login = "ftp://ftp_mydomain:mypassword@mydomain.com/"
file_to_upload = "D:\tmep\myfile.txt"
upload_to_folder = "/myfolder/"
'upload the file
Call Shell("C:\Program Files (x86)\WinSCP\WinSCP.com /log=" & logfile & " /command " & """open """ & ftp_login & " " & """put " & file_to_upload & " " & upload_to_folder & """ " & """exit""")
End Sub
我现在希望 Excel 等到 shell 关闭。
使用来自 Wait for shell command to complete 的信息,我把它放在一起:
Sub FTP_upload_with_wait()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Integer
Dim logfile, ftp_login, file_to_upload, upload_to_folder As String
logfile = "D:\temp\ftp.log"
ftp_login = "ftp://ftp_mydomain:mypassword@mydomain.com/"
file_to_upload = "D:\tmep\myfile.txt"
upload_to_folder = "/myfolder/"
execute_string = "C:\Program Files (x86)\WinSCP\WinSCP.com /log=" & logfile & " /command " & """open """ & ftp_login & " " & """put " & file_to_upload & " " & upload_to_folder & """ " & """exit"""
errorCode = wsh.Run(execute_string, windowStyle, waitOnReturn)
End Sub
不幸的是,这不起作用。 Excel 报告:
run-time error '-2147024894 (80070002)'
Automation error
The system cannot find the file specified
当我用这种方式替换字符串时,它起作用了:
execute_string = "notepad.exe"
看来wsh.Run
不喜欢引号
我怎样才能使这项工作?
WinSCP 的路径包含空格,因此您需要将其用双引号引起来(需要双引号以在 VBA 字符串中对它们进行转义):
execute_string = """C:\Program Files (x86)\WinSCP\WinSCP.com"" ..."
但这只是您命令中错误的第一组引号。
正确的命令如下:
execute_string = """C:\Program Files (x86)\WinSCP\WinSCP.com"" " & _
"/log=" & logfile & " /command " & _
"""open " & ftp_login & """ " & _
"""put " & file_to_upload & " " & upload_to_folder & """ " & _
"""exit"""
假设 logfile
、ftp_login
、file_to_upload
和 upload_to_folder
的 none 包含空格,在这种情况下将需要更多的方法报价。
阅读有关 WinSCP command-line syntax
的矿石
Call Shell
必须有一些启发式方法,可以在 C:\Program Files (x86)\WinSCP\WinSCP.com
周围添加引号。尽管命令行的其余部分可以正常工作纯属幸运,但那里的引号也是错误的。所以即使你的第一个代码也是错误的。它运行以下命令:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /log=D:\temp\ftp.log /command "open "ftp://ftp_mydomain:mypassword@mydomain.com/ "put D:\tmep\myfile.txt /myfolder/" "exit"
(注意 open
周围错位的引号)
我正在使用 Excel 将一些文件上传到带有 WinSCP 的服务器上。 这个例子有效:
Sub FTP_upload()
Dim logfile, ftp_login, file_to_upload, upload_to_folder As String
logfile = "D:\temp\ftp.log"
ftp_login = "ftp://ftp_mydomain:mypassword@mydomain.com/"
file_to_upload = "D:\tmep\myfile.txt"
upload_to_folder = "/myfolder/"
'upload the file
Call Shell("C:\Program Files (x86)\WinSCP\WinSCP.com /log=" & logfile & " /command " & """open """ & ftp_login & " " & """put " & file_to_upload & " " & upload_to_folder & """ " & """exit""")
End Sub
我现在希望 Excel 等到 shell 关闭。
使用来自 Wait for shell command to complete 的信息,我把它放在一起:
Sub FTP_upload_with_wait()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Integer
Dim logfile, ftp_login, file_to_upload, upload_to_folder As String
logfile = "D:\temp\ftp.log"
ftp_login = "ftp://ftp_mydomain:mypassword@mydomain.com/"
file_to_upload = "D:\tmep\myfile.txt"
upload_to_folder = "/myfolder/"
execute_string = "C:\Program Files (x86)\WinSCP\WinSCP.com /log=" & logfile & " /command " & """open """ & ftp_login & " " & """put " & file_to_upload & " " & upload_to_folder & """ " & """exit"""
errorCode = wsh.Run(execute_string, windowStyle, waitOnReturn)
End Sub
不幸的是,这不起作用。 Excel 报告:
run-time error '-2147024894 (80070002)'
Automation error
The system cannot find the file specified
当我用这种方式替换字符串时,它起作用了:
execute_string = "notepad.exe"
看来wsh.Run
不喜欢引号
我怎样才能使这项工作?
WinSCP 的路径包含空格,因此您需要将其用双引号引起来(需要双引号以在 VBA 字符串中对它们进行转义):
execute_string = """C:\Program Files (x86)\WinSCP\WinSCP.com"" ..."
但这只是您命令中错误的第一组引号。
正确的命令如下:
execute_string = """C:\Program Files (x86)\WinSCP\WinSCP.com"" " & _
"/log=" & logfile & " /command " & _
"""open " & ftp_login & """ " & _
"""put " & file_to_upload & " " & upload_to_folder & """ " & _
"""exit"""
假设 logfile
、ftp_login
、file_to_upload
和 upload_to_folder
的 none 包含空格,在这种情况下将需要更多的方法报价。
阅读有关 WinSCP command-line syntax
的矿石Call Shell
必须有一些启发式方法,可以在 C:\Program Files (x86)\WinSCP\WinSCP.com
周围添加引号。尽管命令行的其余部分可以正常工作纯属幸运,但那里的引号也是错误的。所以即使你的第一个代码也是错误的。它运行以下命令:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /log=D:\temp\ftp.log /command "open "ftp://ftp_mydomain:mypassword@mydomain.com/ "put D:\tmep\myfile.txt /myfolder/" "exit"
(注意 open
周围错位的引号)