I have a file which I need to FTP using VBA and I have figured most part of it except last one where I need to insert " in the file name but not able to do.
csvPath = "C:\Users\10613527\Desktop\test\"
sWorkingDirectory = csvPath
sFileToSend = "Price_Change_10-08-15 20-35-49.csv"
iFreeFile = FreeFile
Open sWorkingDirectory & FTP_BATCH_FILE_NAME For Output As #iFreeFile
Print #iFreeFile, "open " & FTP_ADDRESS
Print #iFreeFile, FTP_USERID
Print #iFreeFile, FTP_PASSWORD
Print #iFreeFile, "ASCII"
Print #iFreeFile, "put " & sWorkingDirectory & sFileToSend
Print #iFreeFile, "dir"
Close #iFreeFile
'Shell command the FTP file to the server
Shell "ftp -i -w:20480 -s:" & sWorkingDirectory & FTP_BATCH_FILE_NAME
In the above code , I get the error that the file is not found.
The reason is that the file path and name is not in "", for example this code is writing another script file and executing that one.
So it needs to be
open ftp path
username
password
ASCII
put "C:\Users\10613527\Desktop\test\Price_Change_10-08-15 20-35-49.csv"
dir
and not
open ftp path
username
password
ASCII
put C:\Users\10613527\Desktop\test\Price_Change_10-08-15 20-35-49.csv
dir
Notice the " " in the PUT statement, I have no idea how to place them there.
Use "" to escape " in a vb string.
So
Print #iFreeFile, "put """ & sWorkingDirectory & sFileToSend & """"
Related
I am trying to assign a macro with commandbutton in a Word document which when clicked should open a PDF document of page 9. I trying with the below Code but not successful in solving my Problem.
I am adding the below Code in 'Module 1'.
Private Sub CommandButton1_Click()
App_Path = "C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"
File_Path = "C:\Users\Desktop\USER MANUAL.pdf"
Page_Num = 4
Shell App_Path & " /A Page=" & Page_Num & "" & File_Path, vbMaximizedFocus
End Sub
Because your paths contain spaces both paths need to be enclosed in quotes ""
App_Path = """C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"""
File_Path = """C:\Users\Desktop\USER MANUAL.pdf"""
Also there needs to be a space between Page_Num and File_Path so replace & "" & with & " " &.
Shell App_Path & " /A Page=" & Page_Num & " " & File_Path, vbMaximizedFocus
I have macro running in an Excel sheet. However am getting the error file not found but the files are in the specified directory. See the below code and not sure what we are missing. The error happens when calling SQLLDR .
Shell ("SQLLDR USERID= srvmacro/srvmacroswazi#pn81.world CONTROL=" & outfile & " LOG=" & outfile & ".LOG")
'Shell ("SQLLDR USERID= srvmacro/srvmacroswazi#pn81.world CONTROL=" & outfile & " LOG=" & outfile & ".LOG")
MsgBox " Done all "
End If
'Unload UserForm1
End Sub
Most likely is that you have a space (or other special character in your outfile variable
try the following to see what you are actually trying to execute...
msgbox "SQLLDR USERID=srvmacro/srvmacroswazi#pn81.world CONTROL=" & outfile & ", LOG=" & outfile & ".LOG"
It should probably be more like: -
shell ("SQLLDR USERID=srvmacro/srvmacroswazi#pn81.world CONTROL=""" & outfile & """, LOG=""" & outfile & ".LOG""")
Note the escaped quotes and you are also missing a comma between the keywords
I am having trouble getting the code below to work (from an answer here Using FTP in VBA). I'm in an FTP client from WinSCP and I'm not 100% sure how to use the code. When I run the macro, I get the successful MsgBox "Sent" popup, and I see that a file named whatever I put as FTP_BATCH_FILE_NAME gets created holding the following info:
"open " & FTP_ADDRESS
FTP_USERID
FTP_PASSWORD
"mput " & sWorkingDirectory & sFileToSend
quit
The person who posted the code noted that -
"While stepping through code, you could type the following command in the immediate window and then copy/paste the results into a Command Window:"
? Shell "ftp -i -w:20480 -s:" & sWorkingDirectory & FTP_BATCH_FILE_NAME
But when I do step through the code, and after the yellow highlight passes this line in Excel, I enter this line in and I get an error that '?', sWorkingDirectory, and FTP_BATCH_FILE_NAME is not recognized as an internal or external command.
I thought that after typing it at Cmd line I'd be able to get the contents of FTP_BATCH_FILE_NAME into sFileToSend, but nothing is put into sFileToSend.
Option Explicit
Const FTP_ADDRESS = myHostAddr
Const FTP_USERID = myUsername
Const FTP_PASSWORD = myPassword
Sub Macro1()
If Not SendFtpFile_F() Then
MsgBox "Could not (send/get?) ftp file"
Else
MsgBox "Sent"
End If
End Sub
Function SendFtpFile_F() As Boolean
Dim rc As Integer
Dim iFreeFile As Integer
Dim sFTPUserID As String
Dim sFTPPassWord As String '
Dim sWorkingDirectory As String
Dim sFileToSend As String
Const FTP_BATCH_FILE_NAME = "a file in FTP I want to get.ftp"
Const INCREASED_BUFFER_SIZE = 20480
SendFtpFile_F = False
sWorkingDirectory = "C:\Users\...\Documents\"
sFileToSend = "test.txt" 'is this the file I want to put it into?
On Error GoTo FtpNECAFile_EH
'Kill FTP process file if it exists
If Dir(sWorkingDirectory & FTP_BATCH_FILE_NAME) <> "" Then
Kill sWorkingDirectory & FTP_BATCH_FILE_NAME
End If
'Create FTP process file
iFreeFile = FreeFile
Open sWorkingDirectory & FTP_BATCH_FILE_NAME For Output As #iFreeFile
Print #iFreeFile, "open " & FTP_ADDRESS
Print #iFreeFile, FTP_USERID
Print #iFreeFile, FTP_PASSWORD
Print #iFreeFile, "mput " & sWorkingDirectory & sFileToSend
Print #iFreeFile, "quit"
Close #iFreeFile
'Shell command the FTP file to the server
Shell "ftp -i -w:20480 -s:" & sWorkingDirectory & FTP_BATCH_FILE_NAME
SendFtpFile_F = True
GoTo FtpNECAFile_EX
FtpNECAFile_EH:
MsgBox "Err", Err.Name
FtpNECAFile_EX:
Exit Function
End Function
I hope you can help me with some code here. I've created a module that creates a ftp command file and then a batch file to execute it.
I got this process from another StackOverflow post:
FTP a text file to a server using VBA in Excel
Some of the time test.txt shows up on server, most of the time it doesn't. However, ftp prompt always reports successful transfer.
Private Sub SendFileToServer()
Dim sCmdFile As String
Dim sBatFile As String
Dim vPath As String
Dim iFileNum As Integer
vPath = ThisWorkbook.Path
sCmdFile = vPath & "\" & "ftp" & "\" & "ftpCommand.txt"
iFileNum = FreeFile
Open sCmdFile For Output As iFileNum
Print #iFileNum, "open 555.555.555.55"
Print #iFileNum, "user username"
Print #iFileNum, "password"
Print #iFileNum, "hash"
Print #iFileNum, "ascii"
Print #iFileNum, "lcd " & vPath
Print #iFileNum, "cd dsi_Timesheets"
Print #iFileNum, "put " & vPath & "\" & "test.txt"
Print #1, "close"
Print #1, "quit"
Close #iFileNum
Shell "ftp -n -i -g -s:" & vPath & "\ftp" & "\ftpCommand.txt " & vFTPServ, vbNormalNoFocus
SetAttr vPath & "\ftp" & "\ftpCommand.txt", vbNormal
Kill vPath & "\ftp" & "\ftpCommand.txt"
End Sub
Thanks
Figured out the problem: I'm viewing ftp server files using Filezilla. test.txt wasn't showing up on the ftp server because I was viewing a cached version. By refreshing the ftp directory in Filezilla every time code runs, text.txt is there.
I have a CSV file.Once i click button on csv file.I have to copy that file using ftp into unix server using VB macro.
No clues on this.Please provide any kind of sample.
Regards,
chaitu
I wrote some code to FTP gif files. You can see all the code at
http://www.dailydoseofexcel.com/archives/2006/01/29/ftp-via-vba/
There's a lot more there than you need, but the relevant parts are:
'Create text file with ftp commands
Open sFname & ".txt" For Output As lFnumFtp
Print #lFnumFtp, "open " & sSITE 'open the site
Print #lFnumFtp, sUSER
Print #lFnumFtp, sPASS
Print #lFnumFtp, "binary" 'set file transfer mode
Print #lFnumFtp, "cd " & sDIR
For i = LBound(vFname) To UBound(vFname)
Print #lFnumFtp, "send " & Dir(vFname(i)) 'send files
Next i
Print #lFnumFtp, "bye" 'close ftp session
Close lFnumFtp 'close text file
lFnumBatch = FreeFile
'open a batch file
Open sFname & ".bat" For Output As lFnumBatch
Print #lFnumBatch, "ftp -s:" & sFname & ".txt"
Print #lFnumBatch, "Echo ""Complete""> " & sFname & ".out"
Close lFnumBatch
'run the batch file
Shell sFname & ".bat"