PDA

View Full Version : SFTP Error



dodonohoe
05-06-2014, 02:37 AM
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 (http://winscp.net/eng/docs/library_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 (http://winscp.net/eng/docs/library#example)."


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

dodonohoe
05-06-2014, 02:39 AM
I edited the aboce code with tags

Ago
05-06-2014, 02:50 AM
Is it your real password you just typed out here for everyone to see?
If so I suggest you edit your post ;-)

Bob Phillips
05-06-2014, 03:08 AM
Probably because you haven't referenced that library in the IDE.

snb
05-06-2014, 03:11 AM
You can only start the sub 'upload' from the sub 'example'.
To which reference does 'new session' refer ?

Bob Phillips
05-06-2014, 03:13 AM
You can only start the sub 'upload' from the sub 'example'.


He does!

dodonohoe
05-06-2014, 03:27 AM
Hi xld, thanks for the response which library should I be referring to?

Bob Phillips
05-06-2014, 03:50 AM
The article tells you, WinSCP console interface .NET wrapper , but you have to download and install WinSCP first.

dodonohoe
05-06-2014, 04:17 AM
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.

Jay31
02-21-2017, 07:04 AM
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.