VBA访问FTP进行文件传输的,网络上见到3种方式,用过2种。
一、VBA+DOS批处理的方式,本质上还是通过DOS来进行,有点麻烦,而且运行中会d出CMD窗口,现在已经不用这种方法了。
VBA写DOS:
Open getfd For Output As #1Print #1, "setlocal EnableDelayedExpansion"
Print #1, "("
Print 灶激#1, "echoopen xx.xx.xx.xx"
Print #1, "echouser"
Print #1, "echopwd"
Print #1, "echocd """ ftpfdx8 """"
Print #1, "echoprompt"
Print #1, "echodir"
Print #1, "echobye"
Print #1, ") > """ fdx8 """"
隐弊袜 Print #1,
Print #1, "ftp -v -i -s:""" fdx8 """ | find 卜贺""ftpgroup"" > """ folderx8 """"
Print #1,
VBA运行它:
Shell ("ftp -v -i -s:" & ftpfile)二、VBA调用API,具体是basp21.dll
详见http://www.hi-ho.ne.jp/babaq/eng/basp21f.html,说明很详细,有实例
示例如下,比较简单易懂,其中getfile为下载,上传使用putfile就好了。
Private Sub Form_Load()Dim ftp As Object, rc As Long, v As Variant, v2 As Variant
Dim ctr As Long
Set ftp = CreateObject("basp21.FTP")
ftp.OpenLog "c:\temp\log.txt"
rc = ftp.Connect("ftp.microsoft.com", "anonymous", "")
If rc = 0 Then
v = ftp.GetDir("bussys/winnt/winnt-public", 2)
If IsArray(v) Then
For Each v2 In v
Debug.Print v2
Next
End If
v = ftp.GetDir("bussys/winnt/winnt-public")
If IsArray(v) Then
For Each v2 In v
Debug.Print v2
Next
End If
rc = ftp.GetFile("bussys/winnt/winnt-public/*", "c:\temp")
End If
End
End Sub
1、利用Inet控件上传文件格式:inet1.execute , "put 本地文件 远程文件"
范例:
'初始化Inet控件
Inet1.Protocol = icFTP
Inet1.URL = "ftp://u0501901.k1.13939.org"
Inet1.RemotePort = 21
Inet1.UserName = "u0501901"
Inet1.Password = "750531179574"
Label3.Caption = "正在上传....."
'先关闭当前的数据库存连枝物接
cn.Close
Inet1.Execute , "put" &" " &App.Path &"\vbhelp.mdb" &" " &"/vbhelp/vbhelp.mdb"
Do While Inet1.StillExecuting
DoEvents
Loop
Label3.Caption = "上传成功"
'重新连接数据库
dbconnect
2、利用Inet控件下载文件
格式: inet1.execute , "get 远程文件 本地文件",注意空格缺雀
'伏搭早初始化INET1控件
Inet1.Protocol = icFTP
Inet1.URL = "ftp://u0501901.k1.13939.org"
Inet1.RemotePort = 21
Inet1.UserName = "u0501901"
Inet1.Password = "750531179574"
Label3.Caption = "正在下载...."
'格式: inet1.execute , "get 远程文件 本地文件",注意空格
Inet1.Cancel
Inet1.Execute , "GET /vbhelp/vbhelp.mdb c:\temp\vbhelp.mdb"
Do While Inet1.StillExecuting
DoEvents
Loop
label3.caption="下载成功"
3、利用INET控件建立远程的文件夹
Inet1.AccessType = icUseDefault
Inet1.RemotePort = 21
Inet1.Protocol = icFTP
Inet1.URL = "u0711084.k1.13939.org"
Inet1.UserName = "u0711084"
Inet1.Password = "750531179574"
Inet1.Execute , "mkdir vbtest"
Do While Inet1.StillExecuting
DoEvents
Loop
Inet1.Execute , "close"
Inet1.Execute , "quit"
MsgBox "成功建立文件夹"
4、利用INET控件删除远程文件夹
方法跟建立文件夹一样,删除命令为rmdir,但只能删除空目录,删除文件用delete
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)