VBA运行Unix命令获取运行时错误5

时间:2016-11-01 16:25:19

标签: vba excel-vba excel

我正在尝试让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,无效的过程调用或参数。谁知道为什么??

1 个答案:

答案 0 :(得分:2)

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

如果您使用"选项显式"你必须定义每个变量,并在这种情况下得到错误信息。