Solved

Put csv file on ftp server using vba from excel

Posted on 2008-06-17
3
577 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

856 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