Consulting

Results 1 to 10 of 10

Thread: SFTP Error

  1. #1

    SFTP Error

    I have taken the following code from winscp.net. When I try to compile I get the following error

    "User-defined type not defined" and the following line is highlighted .
    Private Sub Upload(ByRef mySession As Session)Not sure why I am getting this error. The only thing it says about the code on the site is that "This example VBA module (not a class module) is functionally equivalent to overall C# example for WinSCP .NET assembly."


    The full code is here

    Option Explicit 
    Sub Example()
     
        Dim mySession As New Session
        
        ' Enable custom error handling
        On Error Resume Next
        
        Upload mySession
        
        ' 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
        
    End Sub
     
    Private Sub Upload(ByRef mySession As Session)
     
        ' Setup session options
        Dim mySessionOptions As New SessionOptions
        With mySessionOptions
            .Protocol = Protocol_Sftp
            .HostName = "***"
            .UserName = "****"
            .Password = "****"
            .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
        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("d:\toupload\*", "/home/user/", 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
    Last edited by dodonohoe; 05-06-2014 at 03:06 AM.

  2. #2
    I edited the aboce code with tags

  3. #3
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    Is it your real password you just typed out here for everyone to see?
    If so I suggest you edit your post ;-)

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Probably because you haven't referenced that library in the IDE.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You can only start the sub 'upload' from the sub 'example'.
    To which reference does 'new session' refer ?

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by snb View Post
    You can only start the sub 'upload' from the sub 'example'.
    He does!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Hi xld, thanks for the response which library should I be referring to?

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    The article tells you, WinSCP console interface .NET wrapper , but you have to download and install WinSCP first.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Hi XLD, you are spot on. I had everything down loaded but had failed to do the following

    In Microsoft Visual Basic editor, go to Tools > References, check reference to WinSCP console interface .NET wrapper and confirm with OK.

    Thanks very much, I am marking this as solved.

  10. #10
    VBAX Newbie
    Joined
    Feb 2017
    Posts
    1
    Location
    Hi I am using the same code to connect to SFTP Through WinSCP. It gets connected only once.Next time when I run the code I get the error at this line
    "mySession.Open mySessionOptions"

    As Runtime error'-2146233088(80131500)' connection has been unexpectedly closed.Server sent command exit status 0.Authentication failed
    Please Help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •