Some FTP via VBA Questions

localfiend

Board Regular
Joined
Mar 15, 2006
Messages
166
Hi all, I've got a few questions for you. I've been working on a way to transfer pdf files created in excel to my FTP server by using VBA code and have run into a few walls when it comes to error handling.

First off, here's the relevant code:

Code:
'FTP Section

'    Dim serverPATH As String
'    Dim clientPATH As String
        
'        Worksheets("Codes").Range("H25").Value = serverPATH
'        Worksheets("Codes").Range("H18").Value = clientPATH

    ' Build Script File
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\Test\script.dat", True)
        a.writeline "user1" 'username
        a.writeline "password1" 'password
        a.writeline "quote PASV" 'enable passive mode
        a.writeline "put c:\Test\Test.pdf" 'transfer test file
'        a.writeline "cd " & serverPATH 'change server directory
'        a.writeline "put " & clientPATH 'transfer file
        a.writeline "quit" 'quit
        a.Close
     
    ' Build Batch File
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\Test\transfer.bat", True)
        a.writeline "@echo off"
        a.writeline "ftp -i -s:c:\Test\script.dat myftpsite.net"
        a.writeline "echo %errorlevel%" ' 0 = Success
        a.writeline "echo %errorlevel% > check.dat" 'create check.dat
        a.writeline "del script.dat"
        a.writeline ":EOF"
        a.Close
     
    ' Run Batch File
    dRetVal = Shell("C:\Test\transfer.bat", 0) 'transfer the file

The above code works great as far as the transfer goes, even if it is a bit clunky to create a script file that contains users/passwords. The part that does not work is the error logging. The %errorlevel% command I found by googling the subject, but I can't seem to get it to return anything other than 0 even if there is a problem.

Anyone know of a good way to let excel know if the transfer has gone through?

I suppose if worst comes to worst I'll have to find an FTP program that can sync sets of folders with my server at a given time each night - but It would still be nice to create a log showing me what files didn't make the transfer. That way the transfer could be attempted again, or the files in question could be set aside for transfer later.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
there is some code around the forum for Shell and wait for completion, which i looked into in the past for FTP stuff, its only a few more lines than what you have
 
Upvote 0
longshot but maybe in a .bat you need double percent signs:

echo %%errorlevel%%
 
Upvote 0

Forum statistics

Threads
1,216,135
Messages
6,129,075
Members
449,485
Latest member
greggy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top