1

I am trying to develop code for save the file in SFTP server but error in Session and showing error as

user defined type not defined

I am not sure below code working or not and I copied the code from internet source.

Any suggestion would be appreciated.

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
7
  • 1
    Which line returns the error? Commented Dec 21, 2017 at 13:16
  • Did you first follow the steps as outlined here: winscp.net/eng/docs/library_vb ? Commented Dec 21, 2017 at 13:18
  • @destination-data Private Sub Upload(ByRef mySession As Session, ByRef wb1 As Workbook) Commented Dec 21, 2017 at 13:19
  • 1
    As LocEngineer say - I suspect you've missed this: In Microsoft Visual Basic editor, go to Tools > References, check reference to WinSCP scripting interface .NET wrapper and confirm with OK. Commented Dec 21, 2017 at 13:22
  • @LocEngineer - i did that code copied from there only Commented Dec 21, 2017 at 13:23

1 Answer 1

1

Make sure you have followed the instructions for:

Sign up to request clarification or add additional context in comments.

20 Comments

i register below way whereas that library ` WinSCP scripting interface .NET` not available in the Available list in the tool. >C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe "C:\Program Files (x86)\WinSCPWinSCPnet.dll" <C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe "C:\Program Files (x86)\WinSCPWinSCPnet.dll"
Edit your question to show what exactly have you done.
please look above
register successfully done but not showing in the list
Did you restart Excel?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.