WinSCP - Visual Basic for Application use
Automate secure SFTP/FTP file transfers directly from MS Office. Learn how to call the WinSCP DLLs using simple Visual Basic for Application (VBA) code with a practical example.
This article is over a year old but was reviewed on July 30, 2026 — the content is still accurate.
This article is part of the WinSCP & remote file transfer series:
- KeePass - Overriding the URL field
- WinSCP - Retrieve a stored password
- Fatal error was starting Putty after having saved settings
- WinSCP - Synchronize host and remote
- FTP - Remove files and folders at light speed
- WinSCP - Visual Basic for Application use
- WinSCP - Download files with specific extension recursively
- Search your FTP server at lightning speed

This article shows how to call WinSCP's .NET/COM library from VBA (e.g. in Excel) to automate secure file transfers: a Session object connects over SFTP with host, credentials, and SSH host key fingerprint, then PutFiles uploads files, with error handling via On Error Resume Next and a results loop confirming each transfer.
WinSCP is a free, open-source file manager for securely transferring files between your computer and remote servers using protocols like SFTP, FTP, SCP, and WebDAV.
Did you know that you can call WinSCP from ... VBA code?
See https://winscp.net/eng/docs/library_vb#using. There are also several posts in the forum: https://winscp.net/forum/search.php?mode=results
The WinSCP DLLs can be called up through a VBA code and therefore it would be possible to upload / download a file to / from an FTP server.
If VBA isn't a requirement, WinSCP can do the same thing with its own scripting language, which is far simpler; see WinSCP - Synchronize host and remote and WinSCP - Download files with specific extension recursively.
Below a simple VBA example you can use in f.i. MS Excel to upload a file to your remote server.
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 = "example.com"
.UserName = "user"
.Password = "mypassword"
.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("c:\temp\*", "/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
This article is part of the WinSCP & remote file transfer series:
- KeePass - Overriding the URL field
- WinSCP - Retrieve a stored password
- Fatal error was starting Putty after having saved settings
- WinSCP - Synchronize host and remote
- FTP - Remove files and folders at light speed
- WinSCP - Visual Basic for Application use
- WinSCP - Download files with specific extension recursively
- Search your FTP server at lightning speed


