简体   繁体   English

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

[英]VBA run Unix command get run time error 5

I'm trying to let VBA to create a folder on a server. 我试图让VBA在服务器上创建一个文件夹。 The below code works fine: 下面的代码工作正常:

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

However, I want to break this code into two parts, and call them separately from another macro, as follows: 但是,我想将此代码分为两部分,并与另一个宏分开调用,如下所示:

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

Call Shell part gives me run time error 5, Invalid procedure call or argument. 调用Shell部分给我运行时错误5,无效的过程调用或参数。 Anyone knows why?? 有人知道为什么吗?

You define cstrSftp1 in PlinkUserInfo(), and use it in PlinkRunCommand(), but its private in PlinkUserInfo, therefore not defined in PlinkRunCommand. 您可以在PlinkUserInfo()中定义cstrSftp1,并在PlinkRunCommand()中使用它,但在PlinkUserInfo中使用它的私有属性,因此未在PlinkRunCommand中定义。 Then you try to call Shell without real path, just the parameters. 然后,您尝试调用Shell,而不使用实际路径,仅调用参数。

If you use "option explicit" you have to define every variable and would get an error message in this case. 如果使用“显式选项”,则必须定义每个变量,在这种情况下会得到错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM