14

This is my first question ever here on stackoverflow!

I've been searching for a solution to this problem for a while and haven't found any help. I may just be using the wrong keywords in my searches, but so far I've had no luck. Here's the question:

In VBA, how can I select the "Open" option from the file download dialog in Internet Explorer?

Just for extra clarification, I'm talking about the yellow-orange bar that pops up across the bottom of the screen in IE9 when a file is downloaded.

I'm doing some VBA automation to download hundreds of PDFs from the web using Internet Explorer, but there is an intermediate step where a .fdf file has to be opened before I get to the actual PDF. So I first need to select the "Open" option so that I can move on to the next step of the automation. Like I said earlier, I've done a lot of searching and had no luck so far.

I've tried using SendKeys in hopes that hitting Enter would work, and that was a last ditch effort that didn't work.

Thanks in advance for the help!

Tony L.
  • 17,638
  • 8
  • 69
  • 66
Lane Sawyer
  • 370
  • 1
  • 3
  • 16

3 Answers3

4

I have covered this extensively here.

Topic: VBA/VB.Net/VB6–Click Open/Save/Cancel Button on IE Download window – PART I

Link: http://www.siddharthrout.com/2011/10/23/vbavb-netvb6click-opensavecancel-button-on-ie-download-window/

and


EDIT (IMP) If you are using IE 9 Do not forget to read PART 2 as it includes and covers the window structure of IE 9 download window


Topic: VBA/VB.Net/VB6–Click Open/Save/Cancel Button on IE Download window – PART II

Link: http://www.siddharthrout.com/2012/02/02/vbavb-netvb6click-opensavecancel-button-on-ie-download-window-part-ii/

The above links discuss on how to use use the API's to achieve what you want.

From the 1st link...

Like you and me, we both have names, similarly windows have “handles” (hWnd), Class etc. Once you know what that hWnd is, it is easier to interact with that window.

Findwindow API finds the hWnd of a particular window by using the class name and the caption of the window (“File Download”) in this case. The “Open“, “Save” and “Cancel” buttons are windows in itself but they are child windows of the main window which is “File Download“. That means each one of those will also have a hWnd :) To find the child windows, we don’t use FindWindow but use FindWindowEx. All the three buttons “Open“, “Save” and “Cancel” have the same class which is “ Button”.

Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • I'm quite new to VBA, so I'm not sure how to use the FindWindowEx API. I put the function declaration into my code and it said it needed to be updated for a 64 bit system. – Lane Sawyer May 01 '12 at 16:49
  • Just added the PtrSafe attribute, which seemed to fix things. I'll go give it a shot now. – Lane Sawyer May 01 '12 at 16:51
  • I would recommend this link :) http://www.jkp-ads.com/articles/apideclarations.asp – Siddharth Rout May 01 '12 at 16:51
  • `I'll go give it a shot now` Gr8. Lemme know if you get stuck. – Siddharth Rout May 01 '12 at 16:52
  • I was using some of your sample code and got the "Window Not Found" message box. I'm thinking this is because I'm not getting a pop up window, but rather a bar across the bottom of the page. I'm using IE9, if that matters. Any suggestions? – Lane Sawyer May 01 '12 at 16:57
  • Yes that definitely matters. If I am not wrong (And I could be wrong) then you can change that setting so that instead of the bar, it shows you a window? let me check and confirm that. – Siddharth Rout May 01 '12 at 16:59
  • Ok I checked with IE 9 and it doesn't give me the Yellow Bar. Check this for me. In IE, click on Tools icon on extreme left and then click on `Internet Options`. under the security tab, What is the security setting? – Siddharth Rout May 01 '12 at 17:18
  • It was custom. I've started playing with the slider to get the dialog box to pop up, which didn't work. – Lane Sawyer May 01 '12 at 17:27
  • Mine is set at medium high and doesn't give any yellow bar. I am sure there has to be a setting. See if this link helps. http://windows.microsoft.com/en-US/windows7/Internet-Explorer-Information-bar-frequently-asked-questions. I also updated my post above. See the edit. – Siddharth Rout May 01 '12 at 17:32
  • I was able to figure it out. Thanks for the help! – Lane Sawyer May 02 '12 at 19:48
  • hi! Sorry for bringing up an old thread. Where should I put "Call Sample"? I have an URL that doesn't link to the file directly, but after navigating to it, a dialogue box will pop up. I put Call Sample after the readystate is 4, which is exactly when the dialogue box pops up. However, it seems that Call Sample doesn't detect the box (the code halts when the box pops up). Any ideas? – Mariska Jun 26 '14 at 06:13
  • @SiddharthRout I needed to automate Save As on an Internet Explorer download. I had to use your method of moving the cursor to the button and using `mouse_event`. Why do you think the `SendMessage` doesn't work? – CJ7 Mar 12 '16 at 07:02
  • 3
    These links are now dead. :-( Most recent from Wayback's: https://web.archive.org/web/20160323061203/http://www.siddharthrout.com/2011/10/23/vbavb-netvb6click-opensavecancel-button-on-ie-download-window/ and https://web.archive.org/web/20160323160504/http://www.siddharthrout.com/2012/02/02/vbavb-netvb6click-opensavecancel-button-on-ie-download-window-part-ii/ – QHarr May 19 '19 at 05:02
  • I can make them active but I am nit sure if it matters anymore as they worked with old IE @QHarr – Siddharth Rout May 19 '19 at 05:58
  • 1
    Aha. Many thanks. Just someone referred to this question in their comment on an OP’s question today so I came to have a look. Appreciate the response. – QHarr May 19 '19 at 06:00
1

Similar post: link

    Option Explicit
    Dim ie As InternetExplorer
    Dim h As LongPtr
    Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr

Sub Download()
    Dim o As IUIAutomation
    Dim e As IUIAutomationElement
    Set o = New CUIAutomation
    h = ie.Hwnd
    h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
    If h = 0 Then Exit Sub

    Set e = o.ElementFromHandle(ByVal h)
    Dim iCnd As IUIAutomationCondition
    Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Open")

    Dim Button As IUIAutomationElement
    Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
    Dim InvokePattern As IUIAutomationInvokePattern
    Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
    InvokePattern.Invoke
End Sub 
Community
  • 1
  • 1
Valuex
  • 104
  • 1
  • 10
  • This does `Dim o As IUIAutomation` but then later `Set o = New CUIAutomation`, so one of the two seems wrong to me? I know very little about VBA, so I'm not sure which should be adjusted ;-) – Martin Tournoij Sep 04 '16 at 17:55
1

I sent the shortcut keys to the application. Here they are for IE11. Sorry I could not test in IE9. If you hold down Alt, it may show you the other key to the combo as IE11 does.

Note: the code will not run as you expect if IE is not the active window on your machine so it won't work while in debug mode.

  • Shortcut key:Alt+O
  • VBA: Application.SendKeys "%{O}"
Tony L.
  • 17,638
  • 8
  • 69
  • 66