获取"用户定义的类型未定义"尝试在VBA中使用WinSCP .NET程序集将文件上载到SFTP时

时间:2017-12-21 12:40:53

标签: vba excel-vba sftp winscp winscp-net

我正在尝试开发用于在SFTP服务器中保存文件的代码,但在Session中出错并显示错误

  

用户定义的类型未定义

我不确定以下代码是否正常工作,我从互联网资源中复制了代码。

任何建议都将不胜感激。

Sub test()
Dim wb As Workbook
Set wb = "D:\Ashok\Work\Loan_Input_Template V8-Library.xlsx"
Dim mySession As New Session

    ' Enable custom error handling
    On Error Resume Next

    Call Upload(mySession, wb)

    ' Query for errors
    If Err.Number <> 0 Then
        MsgBox "Error: " & Err.Description

        ' Clear the error
        Err.Clear
    End If

    ' Disconnect, clean up
    mySession.Dispose

    ' Restore default error handling
    On Error GoTo 0

            wb.Close SaveChanges:=True

'FileSystemObject.DeleteFile sPathName

End Sub


Private Sub Upload(ByRef mySession As Session, ByRef wb1 As Workbook) 'error line
 Dim wb As Workbook
 Set wb = Workbooks(wb1)
    ' Setup session options
    Dim mySessionOptions As New SessionOptions
    With mySessionOptions
        .Protocol = Protocol_SFTP
        .HostName = "103.231.8.66"
        .UserName = "username"
        .Password = "password"
        .SshHostKeyFingerprint = "ssh-ed25519 256 df:94:44:56:1b:c2:75:8b:b4:58:3a:e2:ef:2e:0d:78"
    End With

    ' Connect
    mySession.Open mySessionOptions

    ' Upload files
    Dim myTransferOptions As New TransferOptions
    myTransferOptions.TransferMode = TransferMode_Binary

    Dim transferResult As TransferOperationResult
    Set transferResult = mySession.PutFiles(wb1, "/home/sftpcf/", False, myTransferOptions)

    ' Throw on any error
    transferResult.Check

    ' Display results
    Dim transfer As TransferEventArgs
    For Each transfer In transferResult.Transfers
        MsgBox "Upload of " & transfer.FileName & " succeeded"
    Next

End Sub

1 个答案:

答案 0 :(得分:1)

确保您已按照以下说明操作: