Solved

Put csv file on ftp server using vba from excel

Posted on 2008-06-17
3
580 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

688 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