Oliver Dewar
Board Regular
- Joined
- Apr 17, 2011
- Messages
- 201
Hi All.
I'm trying to create a macro that will upoad a .csv file to a folder on my website.
I found loads of different version of code to do this and I can't get any of them to work.
I admit right here that I am only as much of an expert in this as Googling for 4 hours can make you... so not
This code seems like the most trusted and best from what I can see:
The .txt file it produces looks like this:
When I run this two files named Directory are created 1 text doc and one bat file. But the code hangs on DoEvents (just after 'Wait for completion) and the file doesn't appear in my website directory.
I think I'm doing something simple wrong but I've tried lots of variations of file addresses etc.
Can anyone see the noobie error I'm making and help me nail it?
Also - assuming I get this working, will I need an extra command to overwrite an existing file of the same name, or will this happen automatically?
All help will be much appreciated.
Cheers,
Oliver
I'm trying to create a macro that will upoad a .csv file to a folder on my website.
I found loads of different version of code to do this and I can't get any of them to work.
I admit right here that I am only as much of an expert in this as Googling for 4 hours can make you... so not
This code seems like the most trusted and best from what I can see:
Code:
Sub PublishFile()
Dim strDirectoryList As String
Dim lStr_Dir As String
Dim lInt_FreeFile01 As Integer
Dim lInt_FreeFile02 As Integer
On Error GoTo Err_Handler
lStr_Dir = ThisWorkbook.Path
lInt_FreeFile01 = FreeFile
lInt_FreeFile02 = FreeFile
'' ANW 07-Feb-2003 :
strDirectoryList = lStr_Dir & "\Directory"
'' Delete completion file
If Dir(strDirectoryList & ".out") <> "" Then Kill (strDirectoryList & ".out")
'' Create text file with FTP commands
Open strDirectoryList & ".txt" For Output As #lInt_FreeFile01
Print #lInt_FreeFile01, "open [URL="ftp://ftp.mysite.com"]ftp.mysite.com[/URL]"
Print #lInt_FreeFile01, "myusername"
Print #lInt_FreeFile01, "mypassword"
Print #lInt_FreeFile01, "cd Website-Search/"
Print #lInt_FreeFile01, "binary"
Print #lInt_FreeFile01, "put """ & ThisWorkbook.Path & "\System\Website-Search-File\Website-Search.csv"""
'' To receive a file, replace the above line with this one
''Print #lInt_FreeFile01, "recv \Picture.gif " & ThisWorkbook.Path & "\Picture.gif"
Print #lInt_FreeFile01, "bye"
Close #lInt_FreeFile01
'' Create Batch program
Open strDirectoryList & ".bat" For Output As #lInt_FreeFile02
Print #lInt_FreeFile02, "ftp -s:" & strDirectoryList & ".txt"
Print #lInt_FreeFile02, "Echo ""Complete"" > " & strDirectoryList & ".out"
Close #lInt_FreeFile02
'' Invoke Directory List generator
Shell (strDirectoryList & ".bat"), vbHide '', vbMinimizedNoFocus
'Wait for completion
Do While Dir(strDirectoryList & ".out") = ""
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:03"))
'' Clean up files
If Dir(strDirectoryList & ".bat") <> "" Then Kill (strDirectoryList & ".bat")
If Dir(strDirectoryList & ".out") <> "" Then Kill (strDirectoryList & ".out")
If Dir(strDirectoryList & ".txt") <> "" Then Kill (strDirectoryList & ".txt")
bye:
Exit Sub
Err_Handler:
MsgBox "Error : " & Err.Number & vbCrLf & "Description : " & Err.Description, vbCritical
Resume bye
End Sub
The .txt file it produces looks like this:
Code:
[SIZE=2]open [URL="ftp://ftp.mysite.com"]ftp.mysite.com[/URL]
[EMAIL="lessons@starsandcatz.com.au"]myusername[/EMAIL]
mypassword
cd Website-Search/
binary
put "C:\Users\User\Dropbox\System\Shells\Australia\System\Website-Search-File\Website-Search.csv"
bye
[/SIZE]
When I run this two files named Directory are created 1 text doc and one bat file. But the code hangs on DoEvents (just after 'Wait for completion) and the file doesn't appear in my website directory.
I think I'm doing something simple wrong but I've tried lots of variations of file addresses etc.
Can anyone see the noobie error I'm making and help me nail it?
Also - assuming I get this working, will I need an extra command to overwrite an existing file of the same name, or will this happen automatically?
All help will be much appreciated.
Cheers,
Oliver
Last edited: