Solved

Put csv file on ftp server using vba from excel

Posted on 2008-06-17
3
573 Views
Last Modified: 2012-08-13
I have a csv file which I have created that I need to transfer to an ftp server that is hosted.  I have opened up other questions trying to get this answer but I guess I am not explaning myself well....and I am new at this.  The file is called costing.csv and it is located on my local drive in the my documents folder.  I am trying to transfer to the ftp server in a folder named test/incoming.  I have found some code on the site which looks like it would accomplish what I need to do but it is not working. Is there somewhere that I can find examples of this?  What does sAgent mean? I would appreciate any help that could be given.  I am assigning higher points to this because I am desperate for help!
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _

    (ByVal sAgent As String, ByVal lAccessType As Long, _

     ByVal sProxyName As String, _

     ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
 

Private Declare Function InternetConnect Lib "wininet.dll" Alias _

    "InternetConnectA" _

    (ByVal hInternetSession As Long, ByVal sServerName As String, _

     ByVal nServerPort As Integer, ByVal sUsername As String, _

     ByVal sPassword As String, ByVal lService As Long, _

     ByVal lFlags As Long, ByVal lContext As Long) As Long
 

Private Declare Function FtpPutFile Lib "wininet.dll" Alias _

    "FtpPutFileA" _

    (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _

    ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _

    ByVal dwContext As Long) As Boolean
 

Sub FTPUpload()

Dim lngINet

Dim lngINetConn

Dim blnRC
 

lngINet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)

lngINetConn = InternetConnect(lngINet, "ftp.mysite.com", 0, _

    "ID", "PASSWORD", 1, 0, 0)

blnRC = FtpPutFile(lngINetConn, "C:\my documents\Costing.csv", "/TEST/incoming/Costing.csv", 1, 0)

End Sub

Open in new window

0
Comment
Question by:ilrosebud26
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 21806837
You have to make sure you close the handle when your finished
Option Explicit
 

Private Declare Function InternetOpenA Lib "wininet.dll" ( _

    ByVal sAgent As String, _

    ByVal lAccessType As Long, _

    ByVal sProxyName As String, _

    ByVal sProxyBypass As String, _

    ByVal lFlags As Long) As Long
 

Private Declare Function InternetConnectA Lib "wininet.dll" ( _

    ByVal hInternetSession As Long, _

    ByVal sServerName As String, _

    ByVal nServerPort As Long, _

    ByVal sUsername As String, _

    ByVal sPassword As String, _

    ByVal lService As Long, _

    ByVal lFlags As Long, _

    ByVal lcontext As Long) As Long
 

Private Declare Function FtpPutFileA Lib "wininet.dll" ( _

    ByVal hConnect As Long, _

    ByVal lpszLocalFile As String, _

    ByVal lpszNewRemoteFile As String, _

    ByVal dwFlags As Long, _

    ByVal dwContext As Long) As Boolean
 

Private Declare Function InternetCloseHandle Lib "wininet" ( _

    ByRef hInet As Long) As Long

    

Sub Upload(ByVal remFile As String, ByVal locFile As String, ByVal Host As String, User As String, Pass As String, ByVal Port As Long)

    Dim hOpen   As Long

    Dim hConn   As Long

    hOpen = InternetOpenA("FTP", 1, vbNullString, vbNullString, 1)

    hConn = InternetConnectA(hOpen, Host, Port, User, Pass, 1, 0, 2)

    FtpPutFileA hConn, locFile, remFile, 0, 0

    InternetCloseHandle hConn

    InternetCloseHandle hOpen

End Sub

Open in new window

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 21806874
The error I belive in your code would be

lngINetConn = InternetConnect(lngINet, "ftp.mysite.com", 0 <--- Maybe this should be set to 21 for typical ftp port
0
 

Author Closing Comment

by:ilrosebud26
ID: 31468108
You are the best!  This worked great.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now