Private bActiveSession As Boolean
Private hOpen As Long
Private hConnection As Long
Private Sub Main()
Call InitVar
Call OpenInternet
If hOpen = 0 Then Exit Sub
Call ConnectInternet
If bActiveSession = False Then
Call DisconnectInternet
Exit Sub
End If
Call GetUpLoadFiles
Call UpLoadNow
Call DisconnectInternet
Call CloseInternet
End Sub
Private Sub InitVar()
bActiveSession = False
hOpen = 0
hConnection = 0
End Sub
Private Sub OpenInternet()
hOpen = InternetOpen(strUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString,vbNullString, 0)
End Sub
Private Sub CloseInternet()
If hConnection <> 0 Then InternetCloseHandle (hConnection)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
hConnection = 0
hOpen = 0
End Sub
Private Sub ConnectInternet()
If bActiveSession = False And hOpen <> 0 Then
If strFtpServer = "" Then Exit Sub
hConnection = InternetConnect(hOpen, strFtpServer, INTERNET_INVALID_PORT_NUMBER, _
strUser, strPassword, INTERNET_SERVICE_FTP, 0, 0)
If hConnection = 0 Then
bActiveSession = False
Else
bActiveSession = True
End If
End If
End Sub
Private Sub DisconnectInternet()
If hConnection <> 0 Then InternetCloseHandle hConnection
hConnection = 0
End Sub
Private Sub UpLoadNow()
Dim i As Integer
Dim strFileLocal As String '要上传的本地文件
Dim strDestFile As String '
Dim bRet As Boolean
If bActiveSession = False Then Exit Sub
bRet = FtpSetCurrentDirectory(hConnection, "\upLoadFiles")
If bRet = False Then
Exit Sub
End If
bRet = FtpPutFile(hConnection, strFileLocal,strDestFile, FTP_TRANSFER_TYPE_BINARY, 0)
End Sub
Private Sub ErrorOutput(dError As Long)
Dim oFSO As FileSystemObject
Dim oTStream As TextStream
Dim dIntError As Long
Dim dLength As Long
Dim strBuffer As String
On Error Resume Next
Set oFSO = New FileSystemObject
Set oTStream = oFSO.OpenTextFile("C:\ErrorMsg.txt", ForAppending, True)
If dError = ERROR_INTERNET_EXTENDED_ERROR Then
InternetGetLastResponseInfo dIntError, vbNullString, dLength
strBuffer = String(dLength + 1, 0)
InternetGetLastResponseInfo dIntError, strBuffer, dLength
If Not oTStream Is Nothing Then
oTStream.WriteLine "[" & Now & "]" & " " & strBuffer
oTStream.Close
End If
End If
Set oTStream = Nothing
Set oFSO = Nothing
End Sub