From: Webbiz on 16 Mar 2010 17:51 I'm trying to write some vb code (to eventually become VBA code within Excel) that can copy a file from my client pc to my linux server and rename it (or, if not possible, to leave the name the same). After much searching on the net, this is what I've come up with so far. Option Explicit Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet 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 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 FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (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 InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean Const FTP_TRANSFER_TYPE_UNKNOWN = &H0 Const FTP_TRANSFER_TYPE_ASCII = &H1 Const FTP_TRANSFER_TYPE_BINARY = &H2 Const INTERNET_DEFAULT_FTP_PORT = 21 ' default for FTP servers Const INTERNET_SERVICE_FTP = 1 Const INTERNET_FLAG_PASSIVE = &H8000000 ' used for FTP connections Const INTERNET_OPEN_TYPE_PRECONFIG = 0 ' use registry configuration Const INTERNET_OPEN_TYPE_DIRECT = 1 ' direct to net Const INTERNET_OPEN_TYPE_PROXY = 3 ' via named proxy Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 ' prevent using java/script/INS Const MAX_PATH = 260 Const PassiveConnection As Boolean = True Public Sub LogToFtp() Dim ftpfolder As String Dim hConnection As Long, hOpen As Long, sOrgPath As String On Error GoTo fout 'open an internet connection hOpen = InternetOpen("", 4, vbNullString, vbNullString, 0) 'connect to the FTP server hConnection = InternetConnect(hOpen, "ftp.mydomain.com", INTERNET_DEFAULT_FTP_PORT, "buffy", "12Vampire", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0) 'create a buffer to store the original directory sOrgPath = String(MAX_PATH, 0) 'get the directory FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath) 'create a new directory 'testing' ftpfolder = "testing" FtpCreateDirectory hConnection, ftpfolder 'set the current directory to 'root/testing' FtpSetCurrentDirectory hConnection, ftpfolder 'upload the file 'myfile.txt' FtpPutFile hConnection, "C:\temp\myfile.txt", "myfile.fdr", FTP_TRANSFER_TYPE_UNKNOWN, 0 'enumerate the file list from the current directory ('root/testing') ' EnumFiles hConnection 'set the current directory back to the root FtpSetCurrentDirectory hConnection, sOrgPath 'close the FTP connection InternetCloseHandle hConnection 'close the internet connection InternetCloseHandle hOpen fout: End Sub Assume the domain name is "mydomain.com" and the file is from "c:\temp\myfile.txt". I want to write this file into a directory called "testing". When I ran the above code, there was no indication that something had indeed happened. So I know that somewhere here I need to be able to return a status as to what has occurred or to say "It worked!" or whatever. However, when I checked the server after running the code, it did in fact create a folder called "testing". However, my file was not copied into this directory as it was empty. The FTP subject via VB (or VBA) is a bit technical for me at this time. If someone knowledgeable about this could comment on what I'm not doing here correctly I would appreciate it. On another note, if it is possible to copy a file to the linux server via VB, then I have to assume it is also possible to copy it back again without the need for PHP or some other intermediary script. Yes? Also, if the directory and/file is already there when I try to copy the file to the server, I need it to 'overwrite' it. Any suggestions, tips, clues, etc? Thanks. Webbiz
From: Karl E. Peterson on 16 Mar 2010 17:54 Webbiz wrote: > When I ran the above code, there was no indication that something had > indeed happened. So I know that somewhere here I need to be able to > return a status as to what has occurred or to say "It worked!" or > whatever. Are you aware of the F8/F9 keys, and how to use them? -- ..NET: It's About Trust! http://vfred.mvps.org
From: Webbiz on 16 Mar 2010 19:22 On Tue, 16 Mar 2010 14:54:35 -0700, Karl E. Peterson <karl(a)exmvps.org> wrote: >Webbiz wrote: >> When I ran the above code, there was no indication that something had >> indeed happened. So I know that somewhere here I need to be able to >> return a status as to what has occurred or to say "It worked!" or >> whatever. > >Are you aware of the F8/F9 keys, and how to use them? Yes Karl. That's not it. I stepped through the darn thing and get nothing. My question was more directed at what I could plug into the code to make it tell me what the server was saying was happening. Again my ingliss not soo goood. :-b Anyway, I'm curious if using the Microsoft Internet Transfer Control is a simplier and better way to go than API for simple file upload and download. So I'm scratching this code and trying out a microsoft example (that isn't working either, but there is less lines :) This is in the Forms (General) Dim objFTP As Inet This is in the Forms_Load() Set objFTP = Me!axFTP.Object (the control is named axFTP) I get this error: Run-time error '13'. Type mismatch. This from a Microsoft MSDN page. Any idea why "Set objFTP = Me!axFTP.Object" causes this error? Thanks. Webbiz
From: Karl E. Peterson on 16 Mar 2010 20:09 Webbiz wrote: > Karl E. Peterson <karl(a)exmvps.org> wrote: > >> Webbiz wrote: >>> When I ran the above code, there was no indication that something had >>> indeed happened. So I know that somewhere here I need to be able to >>> return a status as to what has occurred or to say "It worked!" or >>> whatever. >> >> Are you aware of the F8/F9 keys, and how to use them? > > Yes Karl. That's not it. I stepped through the darn thing and get > nothing. Okay, didn't mean that to sound (too!) condescending. <g> > My question was more directed at what I could plug into the > code to make it tell me what the server was saying was happening. I'd be looking at the return value of each API call. Immediately. Right as/after they were made. > Anyway, I'm curious if using the Microsoft Internet Transfer Control > is a simplier and better way to go than API for simple file upload and > download. Might be. Sorta depends on the guy, I think. For me, it's simpler to take my car in for an oil change. Other guys wouldn't think of doing that. > So I'm scratching this code and trying out a microsoft > example (that isn't working either, but there is less lines :) > > This is in the Forms (General) > > Dim objFTP As Inet > > > This is in the Forms_Load() > > Set objFTP = Me!axFTP.Object > > (the control is named axFTP) > > > I get this error: > > Run-time error '13'. > > Type mismatch. > > This from a Microsoft MSDN page. > > Any idea why "Set objFTP = Me!axFTP.Object" causes this error? The object type being returned isn't the same as the variable you're trying to assign it to. -- ..NET: It's About Trust! http://vfred.mvps.org
From: Webbiz on 16 Mar 2010 21:58
On Tue, 16 Mar 2010 17:09:25 -0700, Karl E. Peterson <karl(a)exmvps.org> wrote: >> >> This is in the Forms (General) >> >> Dim objFTP As Inet >> >> >> This is in the Forms_Load() >> >> Set objFTP = Me!axFTP.Object >> >> (the control is named axFTP) >> >> >> I get this error: >> >> Run-time error '13'. >> >> Type mismatch. >> >> This from a Microsoft MSDN page. >> >> Any idea why "Set objFTP = Me!axFTP.Object" causes this error? > >The object type being returned isn't the same as the variable you're >trying to assign it to. 1. Why then does Microsoft MSDN use this as an example if not correct? 2. Why are they not the same object type? The Inet control is simply named axFTP. The objFTP variable is declared as type Inet. 3. Are you suggesting that the method ".Object" within the Inet control does not return an object of type Inet? Thanks. Webbiz |