Winscp 中的 Session.Open 错误 - VBA

Error on Session.Open in Winscp - VBA

您好,我正在尝试通过 VBA 在 WinSCP 中上传文件。 mySession.Open mySessionOptions 得到了 Error - Network error connection to "103.231.8.66" time out。我用谷歌搜索但没有明白。任何建议将不胜感激

 Sub test()
    Dim wbs As String
     wbs = "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, wbs)

        ' 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 String)  'error line
     Dim wb As Workbook
    Set wb = Workbooks.Open(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 ' ERROR LINE

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

'i am not sure this line will working or not

        Set transferResult = mySession.PutFiles(wb, "/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

错误消息:

手动记录:

终于找到了我的问题的答案:在 mySessionOptions 中添加端口号 .PortNumber = XXXX 并且我删除了 Set wb = Workbooks.Open(wb1)。它完美地工作

WinSCP VBA

WinSCP 端口号默认为正在使用的协议,即 SFTP 端口 22,FTP 端口 21。

协议的会话选项默认为 SFTP,端口为 22。

如果您使用 SFTP,则 sessionOptions 不需要端口号或协议。

Below is an example when default SFTP

    Dim mySessionOptions As New SessionOptions
    With mySessionOptions
        .HostName = "103.231.8.66"
        .UserName = "username"
        .Password = "password"
        .SshHostKeyFingerprint = "ssh-ed25519 256 df......"
    End With