Navigating IE using VBA: interaction with popup “Do you want to open this file?”

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm navigating IE via VBA to reach a web site and download a file. No problem about navigating the site, neverthless I can't interact with the popup “Do you want to open this file?” (I use IE9).

So, I've tried to travel the API road for the first time in my life in order so save the file, but unsuccessfully.


My attempt (in the same module):

Code:
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, _
    ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
Then the code to navigate the site.

Code:
sub navigate ()

'no problem in navigating the site and click on download file'
'then the attempt of managing the popup'

Dim o As IUIAutomation
Dim e As IUIAutomationElement
    
    Set o = New CUIAutomation
    Dim h As Long    
    h = ieApp.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

The error is in the following line (error 404 vba object required)

Code:
h = ieApp.Hwnd


Available Library;
Visual Basic For Applications
Microsoft Excel 12.0 Object Library
OLE Automation
Microsoft Office 12.0 Object Library
Microsoft HTML Object Library
Microsoft Forms 2.0 Object Library
Microsoft DAO 3.6 Object Library
Microsoft Internet Controls
Microsoft VBScript Regulare Expressions 5.5
Microsoft Visual Basic for Application Extensibility 5.3
UIAutomationClient
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
More precisely, this is the pop up (open or save, not only open).

Do you want to open or save this file?
 
Upvote 0
Code:
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 navigate()
  Dim o As IUIAutomation
  Dim e As IUIAutomationElement
  Dim sh
  Dim eachIE

Do

    Set sh = New Shell32.Shell
     For Each eachIE In sh.Windows
   


         If InStr(1, eachIE.LocationURL, "xxxxxxxxxxxxxxxxxx") 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


Nothing to do. I'm still struggling for this.

Now Error 91 (Object variable not set) on the line

Code:
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)

I've added reference UIAutomationClient to my macro file, but I can't copy file C:\Windows\System32\UIAutomationCore.dll file to users Documents i.e C:\Users\admin\Documents because of security restrictions. Could this be the problem?
 
Last edited:
Upvote 0
Sorry for the delayed response. If you are still trying to resolve the issue.

This works for me in IE11, but I had the declaration as below.

Code:
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr


Thanks,
Ko
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,405
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top