Dealing with the open/save/cancel dialogue window in IE when using VBA

不问归期 提交于 2019-12-25 05:13:34

问题


First and foremost, thank you for taking the time and interest into this question. I have been using VBA to automate manual tasks within Excel for sometime now, but just recently started exploring accessing the web using VBA.

Goal: automate file downloads (about 15-20 xlsx files daily) from a website where the file url is nowhere to be found in the page's source code.

Below are the steps that I usually take when downloading these manually.

  1. Open login page and enter login credential to access webpage of interest (i.e. the one with all the reports)
  2. After login in, navigate to the webpage with the report note1: it is setup so that 1 webpage (unique URL) = displays top 55 results in the first page

note2: the same page also has a button to export/save the entire report in different formats

  1. Download the report

  2. Navigate to next webpage (within the same website) and repeat steps 2 and 3 (there's about 15-20 reports/webpages to navigate)

I have gotten as far as downloading the first report by clicking save using the SendKeys. Although sometimes it stops as soon as the dialogue window appears, this has worked up to this point the farthest. It is after this that I have not been able to navigate to another webpage and repeat the same steps to download. My gut feeling is that the Open/Open file/View downloads dialogue window that appears after clicking on the save button is not allowing me to repeat the download/saving process...

I tried looking at the source code of the website to see if I could find the url to the file, but could not find it (not sure if it has to do that the export only occurs after clicking on the submit button which hides the file url or something else like running a script). I'm not very familiar with WinHttpRequest, but seems to be the preferred method after doing my google research. It also looks like this would require to have a file URL, but not sure on this either...

Below is the code that I put together so far. Any help would be very very much appreciated. Thank you! :)

Sub webMacro()

Dim IE As New InternetExplorer
    IE.Visible = True   'change False --> True to open the IE window
    IE.navigate "https://websiteURL.net//apps/login.aspx"

Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE

Dim Doc As HTMLDocument: Set Doc = IE.document

Doc.getElementById("username").Value = "myusername"  'login to the website
Doc.getElementById("pass").Value = "mypassword"
Doc.getElementById("Enter").Click


Sleep (1000)

Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE

IE.navigate "https://firstReportWebPage.net//apps/....."        'navigates to the first webpage (report) to download after login

Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE

Doc.getElementById("##########").Click     'ID of the Export/Save button


Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE


Doc.getElementById("###########").Click     'ID of the Submit button

Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE


Doc.getElementById("############").Click        'ID of the field right before it enters the Open/Save/Cancel dialogue window

Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE


Application.Wait (Now + TimeValue("0:00:02"))  'here I'm using the SendKeys to mimic what I would manually do on the keyboard to get to the "Save" button
   SendKeys "{TAB}", True
   SendKeys "{TAB}", True
   SendKeys "{DOWN}", True
   SendKeys "{ENTER}", True

Sleep (1000)

Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Sleep (1000)


''***This is where it almost always gets stuck...here I'm attempting to get to the Open/Open file/View downloads dialogue window by clicking on the field right before entering the dialogue window using the tab key; same as above when trying to click on the "Save" button in the Open/Save/Cancel dialogue window.

Doc.getElementById("############").Click        'ID of the field right before it enters the Open/Open File/View Downloads dialogue window

Sleep (1000)

Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE

Sleep (1000)

Application.Wait (Now + TimeValue("0:00:02"))

Sleep (1000)
   SendKeys "{TAB}", True
Sleep (1000)
   SendKeys "{TAB}", True
Sleep (1000)
   SendKeys "{TAB}", True
Sleep (1000)
   SendKeys "{TAB}", True
Sleep (1000)
   SendKeys "{ENTER}", True
Sleep (1000)


'some other code to go here...

End Sub

回答1:


I had always seen 'dont use sendkeys' advised by others, but i didnt truly know what they meant when i tried to do something similar to this.

SendKeys will randomly duplicate a key send sometimes (i was using it to control 16 windows at the same time), 1 set of instructions per window and 18,000 instructions that had to be processed.

It happened about 2-3 times for every 500 instructions that were parsed by the browsers, and i couldnt find a workaround.

The navigating the website, i wrote something that does that, and then i also wrote something that downloads the HTML of the page.

Are you able to download the HTML source of the page with the Open/Save/Cancel dialog, and see if the URL to the file exists on that page within the button etc?

If it does, you could perhaps automate navigating to that page, then downloading the HTML (i have code you can have IF the url is in the source), and then parsing the HTML within VBA to calculate the download URL?



来源:https://stackoverflow.com/questions/24472310/dealing-with-the-open-save-cancel-dialogue-window-in-ie-when-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!