0

I am using VBA to automate some operations on the website. Moreover my browser is IE11.

I know that if I want to click "Save" option on the Frame Notification Bar I can use the following chunk of code:

Dim o as IUIAutomation
Set o = New CUIAutomation
Dim h as Long
h = ie.hwnd
Dim ie as InternetExplorer 
ie = CreateObject("InternetExplorer.Application")
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
Dim e as IUIAutomationElement
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

Similarly if I want to click "Open" option I can replace the word "Save" on the code above with the word "Open".

M problem is that after clicking "Save" option, I have 3 another options to click: "Open", "Open folder", "View downloads". I can also close the Frame Notification Bar by clicking the button in the right corner of the Frame Notification Bar. It is visible below:

enter image description here

My question is: How can I click this button using VBA?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
MMM
  • 141
  • 1
  • 5
  • Is there an URL you can share? Anyway we can replicate this process? And what is o in the above? And what references are required to enable IUIAutomationCondition ? – QHarr Oct 08 '18 at 09:39
  • It can be any URL when the Frame Notification Bar appears, e.g. https://finance.yahoo.com/quote/GE/history?p=GE. You have to click 'Download Data' button that is below 'Apply' button. I corrected the code and now definitions of each variable is given. 'UIAutomationClient' reference is needed. – MMM Oct 08 '18 at 10:33
  • Are you using something similar to this? I would simply use the download URL directly via binary download. For the link you supply there is an URL: https://query1.finance.yahoo.com/v7/finance/download/GE?period1=1536404905&period2=1538996905&interval=1d&events=history&crumb=QSTawVcqW3v Then there are no notifications to deal with. – QHarr Oct 08 '18 at 11:10
  • This is very nice solution. Thank you for this. Unfortunately, for the webpage that I use it is not possible to apply this solution, because there is no URL thanks to which the file can be saved. – MMM Oct 08 '18 at 11:30
  • Is there a way to get to something like this? https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.windowpattern.close?view=netframework-4.7.2 – QHarr Oct 08 '18 at 11:32
  • Unfortunately, I do not know how to apply this solution in VBA. – MMM Oct 08 '18 at 12:05
  • I've given a different, easier, solution below. – QHarr Oct 08 '18 at 12:06
  • @MathMen Take a look [at this answer](https://stackoverflow.com/a/43969543/2165759). – omegastripes Oct 08 '18 at 12:10

2 Answers2

0

So I got this to work using the following .

Put the following at the top of the module as usual

Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const WM_DESTROY = &H2
Public Const WM_CLOSE = &H10

Put this after InvokePattern.Invoke, I put the time value there to be safe, may not be necessary.

  Application.Wait Now + TimeValue("00:00:05")
  SendMessage h, WM_CLOSE, 0, 0
Gabriel M
  • 1,486
  • 4
  • 17
  • 25
0

Simply replace "Save" with "Close". So this part of the code will look like this

Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Close")
Set Button = e.FindFirst(TreeScope_Subtree, iCnd) 
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
  • You should edit your code to include a check to see if `e.FindFirst` succeeded or not, just-in-case the `"Close"` button isn't available. You also need to check if the button is enabled or not. – Dai Nov 25 '21 at 21:31
  • It's the educational answer - exactly to the question. – Paweł Jamiołkowski Nov 26 '21 at 16:06