VBA & wininet.dll

J

Jeffery Tyree

Does VBA & wininet.dll just not work together? Below is my ported code
which works under Visual Basic .NET. Anyone see any glaring problems or
know of any secrets, heheh?

TIA,
-Jeff


Option Compare Database
'WININET.DLL function declarations
Public Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal
sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer)
As Integer
Public Declare Function InternetConnect Lib "wininet.dll" Alias
"InternetConnectA" (ByVal hInternetSession As Integer, ByVal sServerName As
String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal
sPassword As String, ByVal lService As Integer, ByVal lFlags As Integer,
ByVal lContext As Integer) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal
hInet As Integer) As Integer
Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA"
(ByVal hFtpSession As Integer, ByVal lpszLocalFile As String, ByVal
lpszRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As
Integer) As Boolean

' system constants aliases
Public Const INTERNET_OPEN_TYPE_DIRECT As Integer = 1
Public Const FTP_TRANSFER_TYPE_BINARY As Integer = 2
Public Const INTERNET_DEFAULT_FTP_PORT As Integer = 21
Public Const INTERNET_SERVICE_FTP As Integer = 1
' code constants for ease of program use
Public Const sAgent As String = "ssc_agent"
Public Const sFTPServer As String = "my.ftp.server"
Public Const sUsername As String = "uname"
Public Const sPassword As String = "pword"

Public Sub Main()
Debug.Print "Program started..."
' Source and destination files
Const sSrcFile As String = "C:\testfile.txt"
Const sDstFile As String = "test_file.txt"
Debug.Print "File transfer started..."
If TransferFile(sSrcFile, sDstFile) Then
Debug.Print "ERROR: File transfer failed. Email file instead."
Else
Debug.Print "File transfer completed!"
End If
Debug.Print "Program finished!"
End Sub

Public Function TransferFile(ByVal sSrcFile As String, ByVal sDstFile As
String) As Integer
' Handle definitions
Dim hOpen As Integer
Dim hConnect As Integer
' temp error
Dim iError As Integer
Dim Success As Boolean
Success = False

' get handle for new Internet session
hOpen = InternetOpen(sAgent, INTERNET_OPEN_TYPE_DIRECT,
vbNullString, vbNullString, 0)
' get handle for connected FTP server
hConnect = InternetConnect(hOpen, sFTPServer,
INTERNET_DEFAULT_FTP_PORT, sUsername, sPassword, INTERNET_SERVICE_FTP, 0, 0)
' transfer file
Success = FtpPutFile(hConnect, sSrcFile, sDstFile,
FTP_TRANSFER_TYPE_BINARY, 0)
' save any error code before closing the session handle
iError = Err.LastDllError
' close Internet session handle
InternetCloseHandle (hOpen)
TransferFile = iError
End Function
 
T

TC

Newsgroups work much better when you give a bit more information. Like,
what actually happens when you run that code. You can't reasonably
expect everyone else to key it in & run it, just to see what happens -
when you could tell us that, in your post.

HTH,
TC
 
J

Jonathan West

Hi Jeffrey,

You need to realise that VB.NET and VBA are very different languages. For
instance, a 4-byte integer is a Integer in VB.NET but a Long in VB, so all
your declare statements are wrong. There may be other problems as well.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jeffery Tyree

Thank you very much for spotting that and sorry I didn't take the time to
provide "info".
Changing the Integer types to Long as you stated solved the problem.

Thanks,
-Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top