使用VBA运行Unix命令与数组获取类型不匹配错误

时间:2016-11-08 21:57:01

标签: vba excel-vba unix excel

我使用VBA连接到UNIX并从服务器下载一系列文件。但是我遇到了"运行时错误13.输入错误"我的" cmd5"错误推荐线。代码如下:

Sub DownloadFirstRunFilesPart2()
Application.StatusBar = "Downloading files..."
Dim wsh As Object
Dim errorcode4 As Integer
Dim cmd5 As Variant
Dim FirstRunFiles(5) As Variant
Dim var As Variant

'Files that need to be downloaded
FirstRunFiles(0) = ProN & "_KSParameter_UserInput.xlsx"
FirstRunFiles(1) = ProN & "_KSParameter_SysOutput.xlsx"
FirstRunFiles(2) = ProN & "_ModelParameter_UserInput.xlsx"
FirstRunFiles(3) = ProN & "_ModelParameter_SysOutput.xlsx"
FirstRunFiles(4) = ProN & "_VarClusParameter_UserInput.xlsx"
FirstRunFiles(5) = ProN & "_VarClusParameter_SysOutput.xlsx"

'Connect to server and run loop to download files. Error occurs in "cmd 5" line
For Each var In FirstRunFiles
cmd5 = Chr(34) & "C:\Program Files (x86)" & "\PuTTY\pscp.exe" & Chr(34) & " -sftp -l " & pUser & " -pw " & pPass & _
    " " & " " & pHost & ":" & ServerPath & "/" & FirstRunFiles(var) & " " & LocalPath & "/"
Set wsh = CreateObject("wscript.shell")
errorcode4 = wsh.Run(cmd5)

'Get feedback and display error message
If errorcode4 <> 0 Then MsgBox ("Error occurs. Fail to download " & FirstRunFiles(var))
Next var

Application.StatusBar = "Download complete"
End Sub
有人可以帮帮我吗?非常感谢!!

1 个答案:

答案 0 :(得分:2)

使用字符串作为数组的索引是无效的,也不需要。

假设您的所有变量都已在代码的其他部分中定义,并且具有模块级范围,则以下轻微更改可能会使您的代码有效。使用:

cmd5 = Chr(34) & "C:\Program Files (x86)" & "\PuTTY\pscp.exe" & Chr(34) & " -sftp -l " & pUser & " -pw " & pPass & _
    " " & " " & pHost & ":" & ServerPath & "/" & var & " " & LocalPath & "/"

而不是:

cmd5 = Chr(34) & "C:\Program Files (x86)" & "\PuTTY\pscp.exe" & Chr(34) & " -sftp -l " & pUser & " -pw " & pPass & _
    " " & " " & pHost & ":" & ServerPath & "/" & FirstRunFiles(var) & " " & LocalPath & "/"