標題:
[發問]
如何用VBA 上傳檔案至FTP?
[打印本頁]
作者:
PKKO
時間:
2016-10-13 23:13
標題:
如何用VBA 上傳檔案至FTP?
各位大大好,小弟想向大家請教上傳的方法
有找了範例的CODE都無法正常執行,不曉得是哪邊的錯誤,還請大大們指教一下
有猜測是我的FTP路徑輸入錯誤
假設我的FTP位置是ftp://test.com
我要放到該位置的下面
那範例的hostFile 要怎麼輸入?hostFile ="ftp://test.com/a.csv" 是這樣嗎?
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
'Connect to the network
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
'Get a file using FTP
Private Declare Function FtpGetFile _
Lib "wininet.dll" _
Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, _
ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, _
ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
'Send a file using FTP
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
'Close the Internet object
Private Declare Function InternetCloseHandle _
Lib "wininet.dll" _
(ByVal hInet As Long) As Integer
Sub UploadFTP()
Dim INet As Long
Dim INetConn As Long
Dim hostFile As String
Dim Password As String
Dim RetVal As Long
Dim ServerName As String
Dim Success As Long
Dim UserName As String
Const ASCII_TRANSFER = 1
Const BINARY_TRANSFER = 2
ServerName = "ftp://test.com"
UserName = "UserName"
Password = "Password"
localFile = "C:\a.csv"
hostFile = "\\a.csv"
RetVal = False
INet = InternetOpen("MyFTP Control", 1&, vbNullString, vbNullString, 0&)
If INet > 0 Then
INetConn = InternetConnect(INet, ServerName, 0&, UserName, Password, 1&, 0&, 0&)
If INetConn > 0 Then
Success = FtpPutFile(INetConn, localFile, hostFile, BINARY_TRANSFER, 0&)
RetVal = InternetCloseHandle(INetConn)
End If
RetVal = InternetCloseHandle(INet)
End If
If Success <> 0 Then
MsgBox ("Upload process completed")
Else
MsgBox "FTP File Error!"
End If
End Sub
複製代碼
作者:
PKKO
時間:
2016-10-13 23:16
這是第二個範例,但也無法執行,想請教變數要如何輸入
我只想把C:\a.csv 的檔案上傳到"ftp://test.com" 的目錄下面而已
Sub SaveToFTPSite()
Dim ftpSite As String
Dim UserID As String
Dim UserPass As String
Dim FTPPath As String
Dim fPathFName As String
Dim ftpWkb As Workbook
ftpSite = "ftp://test.com"
UserID = "UserID"
UserPass = "UserPass"
FTPPath = "upload"
fPathFName = "C:\a.csv"
Application.EnableEvents = False
Application.DisplayAlerts = False
Set ftpWkb = Workbooks.Open(fPathFName)
ActiveWorkbook.SaveAs Filename:="ftp://" & UserID & ":" & UserPass & "@" & ftpSite & "/" & FTPPath & "/" & ftpWkb.Name
ftpWkb.Close
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)