VBA Internet Explorer Automation - How to Select “Open” When Downloading a File

后端 未结 3 414
执笔经年
执笔经年 2020-11-28 13:09

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 usin

相关标签:
3条回答
  • 2020-11-28 13:20

    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 
    
    0 讨论(0)
  • 2020-11-28 13:37

    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”.

    0 讨论(0)
  • 2020-11-28 13:38

    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}"
    0 讨论(0)
提交回复
热议问题