Excel / 如何使用VBA連線FTP 提供了2種透過windows內建ftp程式的連線方式
但是這可能會因為ftp伺服器的關係而無法使用
例如採取加密的ftp伺服器就會因為無法連線而不能夠使用
所以再次爬文,尋找可以在命令列介面(Command-line interface)執行,並且可以使用安全性設定的ftp程式
後來找到WinSCP,可以用命令列(Command-Line)來執行
而且這個程式也提供COM 物件的DLL檔案
因此也可以在C#、VB.net、VBA、PowerShell中使用
本篇先介紹單純使用winscp.com在命令列介面的使用方式
1.先安裝 winscp
會將winscp.com綁進環境變數
2.修改原本的程式
以下的程式碼是連接到使用外顯TLS加密的ftp主機
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Sub FTP_SHELL5()
Dim strPNAME As String '參數文件名
Dim fingerprint As String '文件編號
strPNAME = ThisWorkbook.Path & "\ftptest.txt" '文件名生成
fingerprint = "*************************************************" 'TLS SHA
Dim fsT As Object
Set fsT = CreateObject("ADODB.Stream")
fsT.Type = 2 'Specify stream type - we want To save text/string data.
fsT.Charset = "utf-8" 'Specify charset For the source text data.
fsT.Open 'Open the stream And write binary data To the object
fsT.WriteText "open ftpes://主機名稱-certificate=" & fingerprint & " -rawsettings ProxyPort=0" & Chr(10) '& Chr(13)
fsT.WriteText "帳號" & Chr(10) '& Chr(13)
fsT.WriteText "密碼" & Chr(10) '& Chr(13)
fsT.WriteText "cd WWW" & Chr(10) '& Chr(13) '切換到WWW
fsT.WriteText "cd shares" & Chr(10) '& Chr(13) '切換到shares
fsT.WriteText "get Screenshot_1.jpg " & """" & ThisWorkbook.Path & "\" & """" & Chr(10) '& Chr(13) '下載檔案
fsT.WriteText "close" & Chr(10) '& Chr(13)
fsT.WriteText "exit" & Chr(10) '& Chr(13)
fsT.SaveToFile strPNAME, 2 'Save binary data To disk
Application.Wait (Now + TimeValue("0:00:01")) '程式暫停
Shell "winscp.com /ini=nul /script=" & """" & strPNAME & """", 3 '執行命令
Application.Wait (Now + TimeValue("0:00:03")) '程式暫停 避免還沒下載檔案就刪除命令檔
Dim xFile As String
xFile = Dir(strPNAME)
'Debug.Print xFile
If xFile <> "" Then
Kill strPNAME
End If
End Sub
Dim strPNAME As String '參數文件名
Dim fingerprint As String '文件編號
strPNAME = ThisWorkbook.Path & "\ftptest.txt" '文件名生成
fingerprint = "*************************************************" 'TLS SHA
Dim fsT As Object
Set fsT = CreateObject("ADODB.Stream")
fsT.Type = 2 'Specify stream type - we want To save text/string data.
fsT.Charset = "utf-8" 'Specify charset For the source text data.
fsT.Open 'Open the stream And write binary data To the object
fsT.WriteText "open ftpes://主機名稱-certificate=" & fingerprint & " -rawsettings ProxyPort=0" & Chr(10) '& Chr(13)
fsT.WriteText "帳號" & Chr(10) '& Chr(13)
fsT.WriteText "密碼" & Chr(10) '& Chr(13)
fsT.WriteText "cd WWW" & Chr(10) '& Chr(13) '切換到WWW
fsT.WriteText "cd shares" & Chr(10) '& Chr(13) '切換到shares
fsT.WriteText "get Screenshot_1.jpg " & """" & ThisWorkbook.Path & "\" & """" & Chr(10) '& Chr(13) '下載檔案
fsT.WriteText "close" & Chr(10) '& Chr(13)
fsT.WriteText "exit" & Chr(10) '& Chr(13)
fsT.SaveToFile strPNAME, 2 'Save binary data To disk
Application.Wait (Now + TimeValue("0:00:01")) '程式暫停
Shell "winscp.com /ini=nul /script=" & """" & strPNAME & """", 3 '執行命令
Application.Wait (Now + TimeValue("0:00:03")) '程式暫停 避免還沒下載檔案就刪除命令檔
Dim xFile As String
xFile = Dir(strPNAME)
'Debug.Print xFile
If xFile <> "" Then
Kill strPNAME
End If
End Sub
3.補充說明
1)為了避免寫出的txt檔變成亂碼,因此改用ADODB.Stream物件來生成txt
指定編碼格式為utf-8的文件檔
2)程式碼也可以從 winscp視窗介面中取得
也是透過這裡的程式碼才知道,要改用ftpes,以及TLS的sha碼應該如何設定
0 comments:
張貼留言