I like this small and compact procedure, and use it in my own projects. No temp-files required. Fast and reliable.
Parse a string src
(an absolute filepath) to uploadImageByFTP
. Etc. C:\Users\user\Desktop\image.jpg
, and the file will be uploaded.
Replace:
<username>
with FTP-User
<password>
with FTP-Password
<hostname>
with FTP-hostname (etc. example.com)
<WinSCP.com path>
with path on your WinSCP-client (etc. C:\Program Files (x86)\WinSCP\WinSCP.com
. Caution: WinSCP.com
and not WinSCP.exe
)
<FTP-path>
with path on your FTP-client (etc. /httpdocs/wp-content/uploads
)
Sub uploadImageByFTP(src As String)
Dim script As Object: Set script = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
'Not empty
If (src <> vbNullString) Then
'Execute script
script.Run _
"""<WinSCP.com path>"" " + _
"/ini=nul " + _
"/command " + _
"""open ftp://<username>:<password>@<hostname>/"" " + _
"""cd <FTP-path>"" " + _
"""put " & """""" & src & """""" & """ " + _
"""close"" " + _
"""exit""", windowStyle, waitOnReturn
End If
End Sub
WScript.Shell
is more powerful than the default Shell()
, as you can append a waitOnReturn
-command; this tells VBA, that further execution isn't allowed before the file(s) have been uploaded to the FTP-server.
Change windowStyle
to 0, if you don't like the command prompt to open on each execution.