20170920xlVBA_FTP_UpDownLoad_DownLoad
Posted Excel VBA 小天地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20170920xlVBA_FTP_UpDownLoad_DownLoad相关的知识,希望对你有一定的参考价值。
‘建立应用环境进程 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 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 FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean ‘查找函数 Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long ‘查找数据结构 Private Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As Currency ftLastAccessTime As Currency ftLastWriteTime As Currency nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName As String * 260 cAlternate As String * 14 End Type ‘下载函数 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 ‘上传文件 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 ‘断开链接 Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer Public Function DownLoadFile() As Boolean ‘建立环境 lngINet = InternetOpen("My Ftp", 1, vbNullString, vbNullString, 0) Debug.Print lngINet ‘链接服务器 lngINetConn = InternetConnect(lngINet, host_url, 0, user_name, pass_word, 1, 0, 0) ‘Debug.Print lngINetConn If lngINetConn = 0 Then MsgBox "连接服务器失败!", vbInformation, "NextSeven" DownLoadFile = False GoTo FailTag1 End If ‘切换目录 cngdir = FtpSetCurrentDirectory(lngINetConn, root_folder & sub_folder) ‘目录 If cngdir = False Then MsgBox "切换目录失败!", vbInformation, "NextSeven" DownLoadFile = False GoTo FailTag1 End If Debug.Print cngdir ‘查找首个文件 Dim pData As WIN32_FIND_DATA Dim FileName As String FileName = remote_file lngHINet = FtpFindFirstFile(lngINetConn, FileName, pData, 0, 0) If lngHINet = 0 Then MsgBox "查找文件失败!", vbInformation, "NextSeven" DownLoadFile = False GoTo FailTag1 End If strTemp = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1) blnRc = FtpGetFile(lngINetConn, strTemp, local_file_path, 0, 0, 1, 0) If blnRc Then DownLoadFile = True Debug.Print "下载成功" Else Debug.Print "下载失败" DownLoadFile = False End If FailTag1: InternetCloseHandle lngINetConn FailTag2: InternetCloseHandle lngINet End Function Public Function UpLoadFile() As Boolean ‘建立环境 lngINet = InternetOpen("My Ftp", 1, vbNullString, vbNullString, 0) Debug.Print lngINet ‘链接服务器 lngINetConn = InternetConnect(lngINet, host_url, 0, user_name, pass_word, 1, 0, 0) ‘Debug.Print lngINetConn If lngINetConn = 0 Then MsgBox "连接服务器失败!", vbInformation, "NextSeven" UpLoadFile = False GoTo FailTag1 End If ‘切换目录 cngdir = FtpSetCurrentDirectory(lngINetConn, root_folder & sub_folder) ‘目录 If cngdir = False Then MsgBox "切换目录失败!", vbInformation, "NextSeven" UpLoadFile = False GoTo FailTag1 End If Debug.Print cngdir ‘查找首个文件 Dim pData As WIN32_FIND_DATA Dim FileName As String FileName = remote_file ‘上传文件 blnRc = FtpPutFile(lngINetConn, local_file_path, FileName, 1, 0) If blnRc Then Debug.Print "上传成功" UpLoadFile = True Else Debug.Print "上传失败" UpLoadFile = False End If ‘断开 FailTag1: InternetCloseHandle lngINetConn FailTag2: InternetCloseHandle lngINet End Function
以上是关于20170920xlVBA_FTP_UpDownLoad_DownLoad的主要内容,如果未能解决你的问题,请参考以下文章