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
插入一个控件按钮该按钮的代码如下:
Private Sub CommandButton1_Click()
Dim a As Long
a = Int(InputBox("输入数字"))
ActiveCell.Value = a
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)