Board logo

標題: [發問] 如何用VBA 上傳檔案至FTP? [打印本頁]

作者: PKKO    時間: 2016-10-13 23:13     標題: 如何用VBA 上傳檔案至FTP?

各位大大好,小弟想向大家請教上傳的方法
有找了範例的CODE都無法正常執行,不曉得是哪邊的錯誤,還請大大們指教一下
有猜測是我的FTP路徑輸入錯誤
假設我的FTP位置是ftp://test.com
我要放到該位置的下面
那範例的hostFile 要怎麼輸入?hostFile ="ftp://test.com/a.csv" 是這樣嗎?
  1. Private Declare Function InternetOpen _
  2.    Lib "wininet.dll" _
  3.      Alias "InternetOpenA" _
  4.        (ByVal sAgent As String, _
  5.         ByVal lAccessType As Long, _
  6.         ByVal sProxyName As String, _
  7.         ByVal sProxyBypass As String, _
  8.         ByVal lFlags As Long) As Long

  9. 'Connect to the network
  10. Private Declare Function InternetConnect _
  11.    Lib "wininet.dll" _
  12.      Alias "InternetConnectA" _
  13.        (ByVal hInternetSession As Long, _
  14.         ByVal sServerName As String, _
  15.         ByVal nServerPort As Integer, _
  16.         ByVal sUsername As String, _
  17.         ByVal sPassword As String, _
  18.         ByVal lService As Long, _
  19.         ByVal lFlags As Long, _
  20.         ByVal lContext As Long) As Long

  21. 'Get a file using FTP
  22. Private Declare Function FtpGetFile _
  23.    Lib "wininet.dll" _
  24.      Alias "FtpGetFileA" _
  25.        (ByVal hFtpSession As Long, _
  26.         ByVal lpszRemoteFile As String, _
  27.         ByVal lpszNewFile As String, _
  28.         ByVal fFailIfExists As Boolean, _
  29.         ByVal dwFlagsAndAttributes As Long, _
  30.         ByVal dwFlags As Long, _
  31.         ByVal dwContext As Long) As Boolean

  32. 'Send a file using FTP
  33. Private Declare Function FtpPutFile _
  34.    Lib "wininet.dll" _
  35.      Alias "FtpPutFileA" _
  36.        (ByVal hFtpSession As Long, _
  37.         ByVal lpszLocalFile As String, _
  38.         ByVal lpszRemoteFile As String, _
  39.         ByVal dwFlags As Long, _
  40.         ByVal dwContext As Long) As Boolean

  41. 'Close the Internet object
  42. Private Declare Function InternetCloseHandle _
  43.    Lib "wininet.dll" _
  44.      (ByVal hInet As Long) As Integer

  45. Sub UploadFTP()

  46.   Dim INet As Long
  47.   Dim INetConn As Long
  48.   Dim hostFile As String
  49.   Dim Password As String
  50.   Dim RetVal As Long
  51.   Dim ServerName As String
  52.   Dim Success As Long
  53.   Dim UserName As String
  54.   
  55.   Const ASCII_TRANSFER = 1
  56.   Const BINARY_TRANSFER = 2

  57.     ServerName = "ftp://test.com"
  58.     UserName = "UserName"
  59.     Password = "Password"
  60.     localFile = "C:\a.csv"
  61.     hostFile = "\\a.csv"

  62.       RetVal = False
  63.       INet = InternetOpen("MyFTP Control", 1&, vbNullString, vbNullString, 0&)
  64.         If INet > 0 Then
  65.           INetConn = InternetConnect(INet, ServerName, 0&, UserName, Password, 1&, 0&, 0&)
  66.             If INetConn > 0 Then
  67.               Success = FtpPutFile(INetConn, localFile, hostFile, BINARY_TRANSFER, 0&)
  68.               RetVal = InternetCloseHandle(INetConn)
  69.             End If
  70.          RetVal = InternetCloseHandle(INet)
  71.         End If

  72.       If Success <> 0 Then
  73.         MsgBox ("Upload process completed")
  74.       Else
  75.         MsgBox "FTP File Error!"
  76.       End If

  77. End Sub
複製代碼

作者: PKKO    時間: 2016-10-13 23:16

這是第二個範例,但也無法執行,想請教變數要如何輸入
我只想把C:\a.csv 的檔案上傳到"ftp://test.com" 的目錄下面而已
  1. Sub SaveToFTPSite()
  2. Dim ftpSite As String
  3. Dim UserID As String
  4. Dim UserPass As String
  5. Dim FTPPath As String
  6. Dim fPathFName As String
  7. Dim ftpWkb As Workbook
  8. ftpSite = "ftp://test.com"
  9. UserID = "UserID"
  10. UserPass = "UserPass"
  11. FTPPath = "upload"

  12. fPathFName = "C:\a.csv"

  13. Application.EnableEvents = False
  14. Application.DisplayAlerts = False

  15. Set ftpWkb = Workbooks.Open(fPathFName)
  16.    
  17. ActiveWorkbook.SaveAs Filename:="ftp://" & UserID & ":" & UserPass & "@" & ftpSite & "/" & FTPPath & "/" & ftpWkb.Name

  18. ftpWkb.Close

  19. Application.EnableEvents = True
  20. Application.DisplayAlerts = True
  21.         
  22. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)