vba上传文件到ftp服务器,excel vba upload file to ftp server

这个博客内容涉及到一个使用Excel VBA脚本来上传文本文件到FTP服务器的问题。作者遇到脚本执行失败的困扰,尽管登录详情和FTP服务器地址都是正确的。脚本包括了打开Internet对象、建立连接、FTP文件上传和关闭连接的函数声明,但上传过程并未成功。作者寻求帮助以找出错误所在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am using the following script in excel to try and upload a text file to my server. For some reason my script is failing.

Please can someone show me where I am going wrong? all the login details are correct and my ftp server address is correct. Not sure why it won't work?

I have the script placed in a module and have the module assigned to a active x button control

'Open the Internet object

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 hostFile As String

Dim INet As Long

Dim INetConn As Long

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 = "***"

UserName = "****"

Password = "***"

localFile = "P:\TEST.txt"

hostFile = "/public_html/TEST.txt"

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值