调用internet库里的函数报错,怎么样具体原因?

pingshell 2017-07-15 08:29:09
要用VBA来自动上传一个文件,采用调用 wininet.dll库函数来实现:
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 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 FtpSendFile(ServerIp As String, ServerPort As Integer, UserName As String, Password As String, SubDir As String, LocalFileName As String, RemoteFileName As String, Mode As Integer) As Integer
Dim lSession As Long
Dim lConnect As Long
ChDir (ThisWorkbook.Path)
lSession = InternetOpen("ftp", 1, vbNullString, vbNullString, 0)
If lSession = 0 Then
MsgBox ("打开InternetSession失败!!!")
Debug.Print Err.Description
FtpSendFile = -1
Exit Function
End If
lConnect = InternetConnect(lSession, ServerIp, ServerPort, UserName, Password, 1, 0, 0)
If lConnect = 0 Then
MsgBox ("连接FTP服务器失败!!!")
InternetCloseHandle (lSession)
Debug.Print Err.Description
FtpSendFile = -2
Exit Function
End If
If FtpSetCurrentDirectory(lConnect, SubDir) = False Then
MsgBox ("更改FTP子目录失败!!!")
InternetCloseHandle (lConnect)
InternetCloseHandle (lSession)
Debug.Print Err.Description
FtpSendFile = -3
Exit Function
End If
If FtpPutFile(lConnect, LocalFileName, RemoteFileName, Mode, 0) = False Then
MsgBox ("上传文件失败!!!")
InternetCloseHandle (lConnect)
InternetCloseHandle (lSession)
Debug.Print Err.Description
FtpSendFile = -4
Exit Function
End If
MsgBox ("上传文件成功!")
InternetCloseHandle (lConnect)
InternetCloseHandle (lSession)
FtpSendFile = 0
End Function
总是在调用FtpPutFile时返回假,请问怎么查具体原因?调试的时候这个函数又不能跟进去。打印Err信息也是空的。
...全文
118 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
pingshell 2017-07-17
  • 打赏
  • 举报
回复
已搞定,原来是没有改变当前盘符,要在ChDir前加一个ChDrive Split(ThisWorkbook.Path, ":")(0)改变当前盘符。

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧