我写了一些VBA代码,通过shell命令调用ftp.exe
。代码运行时,shell命令不会执行。但是,如果我在调试模式下遍历代码,则它每次都有效。下面是代码:运行shell命令的VBA只能在调试模式下工作
Sub FTPFile(sSrc As String)
Dim sHost As String
Dim sUser As String
Dim sPass As String
Dim sDest As String
Dim sFTPCmds As String
Dim strConnect As String
'Build up the necessary parameters
sHost = "<redacted>"
sUser = "<redacted>"
sPass = "<redacted>"
sDest = "\"
'Write the FTP commands to a text file
iFNum = FreeFile
sFTPCmds = "<path redacted>" & "FTPCmd.tmp"
Open sFTPCmds For Output As #iFNum
Print #iFNum, "op " & sHost
Print #iFNum, "user " & sUser & " " & sPass
Print #iFNum, "cd " & sDest
Print #iFNum, "put " & sSrc
Print #iFNum, "bye"
Close #iFNum
Shell Environ("WINDIR") & "\System32\ftp.exe -n -s:" & sFTPCmds
End Sub
我想出的唯一可能的想法是,调用该命令的用户权限不同,根据是否在调试模式或运行时出现的电话,但我不确定如何改变命令。
感谢您的建议。当我尝试时,我找不到错误53文件。因此,我尝试了如下变种: Shell“”&Environ(“WINDIR”)&“\ System32 \ ftp.exe -n -s:”&sFTPCmds&“”导致了同样的问题面向,并且 Shell“'”&Environ(“WINDIR”)&“\ System32 \ ftp。exe -n -s:“&sFTPCmds&”'“这也产生了错误53文件not found。 –
我没有这方面的经验,但总的来说,如果某些东西在调试时有效,但不是代码运行时,那么这可能是一个时间问题,添加到“找不到文件”,让我怀疑你创建的文本文件是否没有完全准备好,并在执行Shell时从锁定释放?如果将一个无意义的for-loop在那里通过高数来计算系统时间来处理文件? –
'Debug.Print' Shell命令并在PowerShell或命令提示符中运行该行注意路径名称中的空格需要引号括号 – Parfait