vbaexcelwinscp

How to use Excel VBA to run WinSCP Script?


I wrote a WinSCP script that upload a local file to a SFTP site. The script is saved into .txt:

WinSCP.com
open sftp://username:password@address
cd /export/home/Desktop/Temp
put C:\Users\Dekstop\JPMC\a.xlsx
close
exit

Then I look at Using VBA to run WinSCP script, and write this code into Excel VBA:

Sub RunWinScp()
  Call Shell("C:\Program Files (x86)\WinSCP\WinSCP.com /ini=nul/script=C:\Users\Desktop\WinSCPGetNew.txt")
End Sub

But when I try to run it, nothing happens. There is no error, but the file is not transferred correctly neither. Could someone please help?

Thanks a lot!


Solution

  • Call Shell("""C:\Program Files (x86)\WinSCP\WinSCP.com"" /ini=nul /script=C:\Users\Desktop\WinSCPGetNew.txt")
    

    Though you better specify the commands on WinSCP command-line using the /command switch to avoid a need to for a separate commands file:

    Call Shell( _
        """C:\Program Files (x86)\WinSCP\WinSCP.com"" " + _
        "/ini=nul " + _
        "/command " + _
        """open sftp://username:password@example.com/"" " + _
        """cd /export/home/Desktop/Temp"" " + _
        """put C:\users\Desktop\JPMC\a.xlsx"" " + _
        """close"" " + _
        """exit""")