问题
I have a question regarding a topic that is already discussed in some other threads and forums but I do not manage to make it work for me. So I came here to ask that questions concerning my individual code.
Basically, I access an intranet-site and based on some input (via checkboxes) a report is created with data from SAP.
My problem arises after the report is generated and IE prompts me to press the "save" button on its dialog box. I do not manage to automate that part.
Could you help me here? I would like to store the report in the "Downloads" Folder.
You'll find my code below. Due to compliance reasons I cannot show the original URL.
Any help is widely appreciated.
Best 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
回答1:
Generally, When download file from IE browser, it will display a prompt to let user click the Save button. we could use the Application.SendKeys "%{s}" command to click the Save button in VBA.
Sample code as below:
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 page resource:
<a id="btnDowloadReport" href="https://research.google.com/pubs/archive/44678.pdf" download>Download</a>
[Note] Please check your IE browser setting, make sure the Download path is the "Downloads" Folder.
来源:https://stackoverflow.com/questions/61385991/excel-vba-access-website-generate-report-press-save-on-ie-dialog-bar