Excel VBA - 访问网站,生成报告并在IE对话框栏上按保存



我有一个关于某个主题的问题,这个主题已经在其他一些线程和论坛中讨论过了,但我无法让它为我工作。所以我来这里是想问一些关于我个人代码的问题。

基本上,我访问一个intranet站点,并根据一些输入(通过复选框(使用SAP的数据创建一个报告。

我的问题出现在生成报告后,IE提示我按下对话框上的"保存"按钮。我没能把那个部分自动化。

你能帮我一下吗?我想将报告存储在"下载"文件夹中。

你会在下面找到我的代码。由于合规性原因,我无法显示原始URL。

任何帮助都会受到广泛的赞赏。

最佳Simon


Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim i As Integer
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.navigate "https://blablablablablabla"
Application.Wait (Now + TimeValue("00:00:03"))
Set html = ie.document
html.getElementById("ctl00_MainContent_RadCboRepFilter1_Arrow").Click
Application.Wait (Now + TimeValue("00:00:01"))
html.getElementsByClassName("rcbList")(0).Children(5).Click
Application.Wait (Now + TimeValue("00:00:01"))
html.getElementById("ctl00_MainContent_RadCboRepFilter2_Arrow").Click
Application.Wait (Now + TimeValue("00:00:01"))
html.getElementsByClassName("rcbList")(0).Children(0).Click
Application.Wait (Now + TimeValue("00:00:01"))
html.getElementById("ctl00_MainContent_RadCboListOppStatus_ctl01").Click
html.getElementById("ctl00_MainContent_RadCboListOppStatus_ctl02").Click
html.getElementById("ctl00_MainContent_RadcboListSalesStage_ctl00").Click
html.getElementById("ctl00_MainContent_RadcboListSalesStage_ctl01").Click
html.getElementById("ctl00_MainContent_RadcboListSalesStage_ctl02").Click
Application.Wait (Now + TimeValue("00:00:01"))
html.getElementById("MainContent_BtnRunReport").Click
End Sub

通常,从IE浏览器下载文件时,会显示提示,让用户单击"保存"按钮。我们可以使用该应用程序。SendKeys"%{s}"命令单击VBA中的"保存"按钮。

示例代码如下:

Sub Test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "<the website url>"
While IE.ReadyState <> 4
DoEvents
Wend
'click the button to download the file.
IE.Document.getElementbyId("btnDowloadReport").Click
'wait the download prompt appear
Application.Wait (Now + TimeValue("00:00:03"))
Application.SendKeys "%{s}"
'Waiting for the site to load.
End With
Set IE = Nothing
End Sub

Html页面资源:

<a id="btnDowloadReport" href="https://research.google.com/pubs/archive/44678.pdf" download>Download</a>

[注意]请检查您的IE浏览器设置,确保下载路径为"下载"文件夹。

最新更新