我有这段代码可以从Internet上使用Windows 10 64bit的EXCEL2013 VBA 64bit下载文件。它可以在防火墙关闭的情况下工作,但在防火墙处于活动状态时则不能。 我试图为urlmon.dll设置防火墙例外,但这并不能解决任何问题。有什么好主意的人吗?
Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub DownloadFile(dateString As String)
Dim DownloadFile As String, LocalFilename As String, URL As String, extraString As String
Dim result As Long
DownloadFile = dateString + ".xlsx"
URL = Cells(1, 1).text
If (InStr(URL, "/") <> 0) Then
extraString = Mid$(URL, InStrRev(URL, "/") + 1)
If (InStr(extraString, "_") <> 0) Then
extraString = Mid$(extraString, 1, InStrRev(extraString, "_"))
URL = Mid$(URL, 1, InStrRev(URL, "/"))
URL = URL + extraString + DownloadFile
End If
End If
LocalFilename = Application.ActiveWorkbook.path + Application.PathSeparator + dataFolderName + Application.PathSeparator + DownloadFile 'test.txt"
Dim folderString As String
folderString = Mid$(LocalFilename, 1, InStrRev(LocalFilename, Application.PathSeparator))
If ((InStr(LocalFilename, Application.PathSeparator) <> 0) And (FileExists(folderString) = False)) Then
CreateFolder (folderString)
End If
result = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If (result <> 0) Then
MsgBox "Error downloading webpage, check firewall/adress", vbOKOnly
End If
End Sub