I am trying to download an excel sheet from a website. I have thus far achieved until clicking the download button automatically (web scraping). Now ie9 is popping a save as screen. How do i automate that?
Asked
Active
Viewed 1.6k times
3 Answers
12
You may try this as it is worked for me on IE9:
- Copy file
C:\Windows\System32\UIAutomationCore.dll
file to users Documents i.eC:\Users\admin\Documents
then add referenceUIAutomationClient
to your macro file. Paste below code in your module:
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, "Save") 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
Try at your end.

Lifewithsun
- 968
- 14
- 34
-
1When i am trying to add refernence to UIAutomationClient it's throwing a error : "Error in loading DLL". – ramses1592 Nov 21 '14 at 13:42
-
2you have to copy the file `UIAutomationCore.dll` to your document folder. – Lifewithsun Nov 21 '14 at 15:17
-
1`PtrSafe` is not getting recognized. It says *Expected: Sub or Function* – Zameer Ansari Sep 15 '15 at 11:15
-
1Caution! If you are foreigner then you should properly change button name instead of "Save" like "Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "열기")" – JonghoKim Sep 21 '16 at 05:35
-
How to close window after `If h = 0 Then Exit Sub` ? – Dmitrij Holkin Oct 15 '18 at 08:35
1
'This is a working code for vba in excel 2007 to open a file
'But you need to add the "UIAutomationCore.dll" to be copied
'from "C:\Windows\System32\UIAutomationCore.dll" into the
'path "C:\Users\admin\Documents"
'The path where to copy may be different and you can find it when you check on
'the box for UIAutomationClient - the location is given under it.
'Tools-references
Option Explicit
Dim ie As InternetExplorer
Dim h As LONG_PTR
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LONG_PTR, ByVal hWnd2 As LONG_PTR, ByVal lpsz1 As String, ByVal lpsz2 As String) As LONG_PTR
Sub click_open()
Dim o As IUIAutomation
Dim e As IUIAutomationElement
Dim sh
Dim eachIE
Do
Set sh = New Shell32.Shell
For Each eachIE In sh.Windows
' Check if this is the desired URL
' Here you can use your condition except .html
' If you want to use your URL , then put the URL below in the code for condition check.
' This code will reassign your IE object with the same reference for navigation and your issue will resolve.
If InStr(1, eachIE.LocationURL, "<enter your page url>") Then
Set ie = eachIE
Exit Do
End If
Next eachIE
Loop
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

Tuhinansu Gourav
- 11
- 2
0
I sent the shortcut keys to IE11.
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. The shortcut keys and how to send them are below.
- Shortcut key:Alt+S
- VBA:
Application.SendKeys "%{S}"

Tony L.
- 17,638
- 8
- 69
- 66