gpt4 book ai didi

Call .com file with parameters using VBA WScript.Shell(使用VBA WScript.Shell使用参数调用.com文件)

转载 作者:bug小助手 更新时间:2023-10-24 20:35:59 30 4
gpt4 key购买 nike



I'm using Excel to upload some files onto an server with WinSCP.
This example works:

我正在使用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:[email protected]/"
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


I now want Excel to wait until the shell has closed.

现在,我希望Excel等到外壳关闭。



Using the information from Wait for shell command to complete, I put it together this code:

使用来自等待外壳命令完成的信息,我将其组合成以下代码:



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:[email protected]/"
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


Unfortunately, this doesn't work. Excel reports:

不幸的是,这并不管用。Excel报告:




run-time error '-2147024894 (80070002)'

Automation error

The system cannot find the file specified




When I replace the string this way, it works:

当我这样替换字符串时,它起作用了:



execute_string = "notepad.exe"


It seems that wsh.Run doesn't like the quotation marks.

How can I make this work?

看起来wsh.run不喜欢这些引号。我怎么才能让这件事奏效呢?


更多回答
优秀答案推荐

The path to WinSCP contains spaces, so you need to wrap it to double-quotes (which need to be doubled to escape them in VBA string):

指向WinSCP的路径包含空格,因此需要用双引号将其括起来(需要用双引号将其转义为VBA字符串):



execute_string = """C:\Program Files (x86)\WinSCP\WinSCP.com"" ..."


But that's only the first set of quotes that is wrong in your command.

但这只是你的命令中错误的第一组引语。



The correct command would be like:

正确的命令应如下所示:



execute_string = """C:\Program Files (x86)\WinSCP\WinSCP.com"" " & _
"/log=" & logfile & " /command " & _
"""open " & ftp_login & """ " & _
"""put " & file_to_upload & " " & upload_to_folder & """ " & _
"""exit"""


Assuming that none of logfile, ftp_login, file_to_upload and upload_to_folder contains spaces, in which case would would need a way more double-quotes.

假设日志文件、ftp_登录、文件_到_上载和上载_到_文件夹都不包含空格,在这种情况下,将需要更多的双引号。



Read ore about WinSCP command-line syntax

阅读有关WinSCP命令行语法的文章






The Call Shell must have some heuristics that adds the quotes around C:\Program Files (x86)\WinSCP\WinSCP.com. Though it's just a pure luck that the rest of the command-line works, the quotes are wrong there too. So even your first code is wrong. It runs the following command:

调用外壳必须有一些启发式方法,在C:\Program Files(X86)\WinSCP\WinSCP.com周围添加引号。尽管命令行的其余部分能够正常工作纯粹是运气使然,但其中的引号也是错误的。因此,即使是您的第一个代码也是错误的。它运行以下命令:



"C:\Program Files (x86)\WinSCP\WinSCP.com" /log=D:\temp\ftp.log /command "open "ftp://ftp_mydomain:mypa[email protected]/ "put D:\tmep\myfile.txt /myfolder/" "exit"


(Note the misplaced quotes around open)

(注意OPEN前后错误的引号)



    Dim objShell, oExec
Set objShell = CreateObject("wscript.shell")
Set oExec = objShell.Exec(".....")
Status = oExec.Status
'waiting for return.
While oExec.Status <= 0
Wend
ProcessID = oExec.ProcessID
ExitCode = oExec.ExitCode

更多回答

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

正如它目前所写的,你的答案并不清楚。请编辑以添加更多详细信息,以帮助其他人了解这是如何解决提出的问题的。你可以在帮助中心找到更多关于如何写出好答案的信息。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com