【问题标题】:VBA run Unix command get run time error 5VBA 运行 Unix 命令获取运行时错误 5
【发布时间】:2016-11-02 00:53:26
【问题描述】:

我试图让 VBA 在服务器上创建一个文件夹。下面的代码工作正常:

Public pUser As String
Public pPass As String
Public pHost As String
Public cmd2 As Variant

Sub PlinkUserInfo()
Const cstrSftp1 As String = "C:\Program Files (x86)\PuTTY\plink.exe"
pUser = InputBox("Please enter your Putty username")
pPass = InputBox("Please enter your Putty password")
pHost = Workbooks("Robot Model.xlsm").Worksheets("Preparation").Range("C6").Value
pCommand = "cd /busbank/home; mkdir test3"
cmd2 = cstrSftp1 & " -l " & pUser & " -pw " & pPass & " -P 22 -2 -ssh " & pHost & " " & pCommand
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStlye As Integer: windowStyle = 1
Debug.Print cmd2
Call Shell(cmd2, vbNormalFocus)
End Sub

但是,我想把这段代码分成两部分,和另一个宏分开调用,如下:

Public pUser As String
Public pPass As String
Public pHost As String
Public cmd2 As Variant

Sub PlinkUserInfo()
Const cstrSftp1 As String = "C:\Program Files (x86)\PuTTY\plink.exe"
pUser = InputBox("Please enter your Putty username")
pPass = InputBox("Please enter your Putty password")
pHost = Workbooks("Robot Model.xlsm").Worksheets("Preparation").Range("C6").Value
End Sub

Sub PlinkRunCommand()
cmd2 = cstrSftp1 & " -l " & pUser & " -pw " & pPass & " -P 22 -2 -ssh " & pHost & " " & pCommand
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStlye As Integer: windowStyle = 1
Debug.Print cmd2
Call Shell(cmd2, vbNormalFocus)
End Sub

Public pCommand As Variant

Sub SetUpRemoteFolder()
Call PlinkUserInfo
pCommand = "cd /busbank/home; mkdir test3"
Call PlinkRunCommand
MsgBox ("The server folder is successfully created.")
End Sub

调用 Shell 部分给我运行时错误 5,无效的过程调用或参数。有谁知道为什么?

【问题讨论】:

  • 你没有引用你的路径,所以它试图运行一个名为c:\program的程序,参数为files (x86)\putty\plink.exe

标签: vba excel


【解决方案1】:

您在 PlinkUserInfo() 中定义 cstrSftp1,并在 PlinkRunCommand() 中使用它,但它在 PlinkUserInfo 中是私有的,因此未在 PlinkRunCommand 中定义。然后你尝试在没有真实路径的情况下调用 Shell,只需要参数。

如果您使用“选项显式”,则必须定义每个变量,并且在这种情况下会收到错误消息。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多