由于VBA中IE 11的失效,下载网页的方法是什么



我在Excel中有一个广泛的VBA宏,用于打开IE URL并下载页面。它使用CCD_ 1和CCD_;urlmon";。此宏的目的是执行以下操作:
从"导出"工作表获取URL,调用SSRS(Reporting Services(以显示报告,并将报告下载到具有"导出"表上提供的路径和名称的文件中。这将生成一组文件,每个报告一个,文件中包含参数、图表和数据表

以下是宏中的相关代码片段:

If Cells(ActiveCell.row, 3).Value = "" Then
Call IE_Automation0(URL & "&rs:Command=Render&rs:Format=EXCEL&rc:Toolbar=false", Cells(7, 9).Value + IIf(Right(Cells(7, 9).Value, 1) = "", "", "") + Cells(ActiveCell.row, 2).Value) ' change 20120327        
Else
Call IE_Automation0(URL & "&rs:Command=Render&" & theFormat & "&rc:Toolbar=false", Cells(ActiveCell.row, 3).Value + IIf(Right(Cells(ActiveCell.row, 3).Value, 1) = "", "", "") + Cells(ActiveCell.row, 2).Value) ' change 20120327
End If

以下内容在IE_Automation0:中

' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' Send the form data To URL As POST binary request
IE.Navigate theURL
Call downloadFile(theURL, theSaveAsFilename + theExtenstion) ' download the SSRS file and save it

这是在downloadFile函数中,该函数调用urlmon:的URLDownloadToFile函数

returnVal = URLDownloadToFile(0, target, strSavePath, 0, 0)

由于IE将于2022年6月被删除,我需要做些什么来更改我的代码,以便我可以使用Edge、Chrome或Firefox执行相同的功能。

派对迟到了一点。但如果有人需要解决方案,这可能仍然有效。

声明模块中的DLL。

Private Declare Function ShellExecute _
Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal Operation As String, _
ByVal Filename As String, _
Optional ByVal Parameters As String, _
Optional ByVal Directory As String, _
Optional ByVal WindowStyle As Long = vbMinimizedFocus _
) As Long

然后创建要使用的函数:

Function download(URL, ToFile) As Boolean
On Error GoTo feil
download = False
' Parameter for funsjon overføres
strFileURL = URL                    ' eks "http://norsktipping.n3sport.no/default.aspx?event=GETMATCHINFO&NTMatchId=209562"
strHDLocation = ToFile              ' eks "file.htm"

' Hent filen
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

objXMLHTTP.Open "GET", strFileURL, False
sv = objXMLHTTP.send()

If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary

objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0    'Set the stream position to the start

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
download = True
End If

Set objXMLHTTP = Nothing
Exit Function
feil:
If ActiveWorkbook.ReadOnly Then MsgBox ("Workbook is in read only mode! This result in error when using the dll for web.")

download = False
Resume Next
End Function

然后你可以调用任何你想下载的文件URL,如下所示:

Sub testDownloadOfImage()
sv = download("http://s3-eu-west-1.amazonaws.com/db-comics/24_1984_web", "C:TempTestImage.bmp")
End Sub

最新更新