How to get handle of menu in external application, and find button where no text is part of class.

Galaxea

New Member
Joined
Jul 20, 2018
Messages
34
Office Version
  1. 2021
Platform
  1. Windows
@Jaafar Tribak - I have seen that you have extensive experience in this topic so I am hoping that you can help me please.

I have an external application that I can launch and control from Excel, but only up to a point.
As soon as I click on the SEND TO button and the menu pops up I can't go any further as I cannot get the handle or any other info.
Even bringing up the SPY++ tool will make the menu disappear as soon as the focus is changed off the menu.

Please contact me, even off the forum if necessary as I really need to get this working! I can't see a way to contact someone directly on this forum.

1658424498840.png


I have another similar issue with the latest version of the same program (I am in the process of upgrading to it) where I can't find enough info to know what to click to open a company file (see below).
Thanks. Rob.

1658425416226.png
 
That error suggests ContextMenu is Nothing, or MenuItems is Nothing or its length is 0 (MenuItems.Length property), meaning ContextMenu.FindAll didn't find the "Context" menu control. Click Debug; is ContextMenu Nothing? If not, is MenuItems Nothing? Try the Desktop.FindFirst and/or ContextMenu.FindAll with TreeScope_Descendants instead of TreeScope_Children. (A lot of this is trial and error to find what works).

Assuming you get MenuItem3 to reference the 3rd menu item, click it with either of these:

VBA Code:
    Dim InvokePattern As IUIAutomationInvokePattern
      
    Set InvokePattern = MenuItem3.GetCurrentPattern(UIA_InvokePatternId)
    MenuItem3.SetFocus
    InvokePattern.Invoke
Or:
VBA Code:
    'Using the legacy pattern, which may have the DoDefaultAction method (Click)
  
    Dim LegacyPattern As IUIAutomationLegacyIAccessiblePattern

    Set LegacyPattern = MenuItem3.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
    MenuItem3.SetFocus
    LegacyPattern.DoDefaultAction
    'Or, another way of clicking
    'LegacyPattern.Select 2

Unfortunately, intellisense editing is not available with UIAutomation, so you can't see the properties and methods available when you type the dot after an object variable. The best way to see the classes and members is with the VBA object browser (press the F2 key in the VBA editor). Select UIAutomationClient in the libraries dropdown and enter any partial or full class name or member:

1658833434102.png
 
Last edited:
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
All of the previous objects are set (not Nothing).
I have tried all combinations of Children/Descendants on both of those lines but I get the same result...
Anything else I can try please?

1658835299506.png
 
Upvote 0
It is 3.
I just tried to set "MenuItems.GetElement(2)" instead of (3) and it worked...
I will try the code to click the item next.
Woohoo- nearly there I hope.
 
Upvote 0
John, I owe you a beer/coffee!
It works.
I still need to get this working on the new version of the application that I am subclassing, but I am sure that I can do it now.
Please send me a link to your "buymeacoffee"/"donatetome" app if you have one. I will happily send you something for your assistance.
REALLY appreciate it - thanks.
PS: where are you/what time zone are you in?
 
Upvote 0
Do you have any suggestions for traversing a list of children where they all have the same "name"? I am looking for the seventh one below that has the YES button on it.
I can count the children until I have found the seventh one and then click on the button, but I was wandering if there is a better way (UIAutomation, or something else).
Just in case the button is not always the seventh child.
Thanks.

1658858983358.png
 
Upvote 0
Do you have any suggestions for traversing a list of children where they all have the same "name"? I am looking for the seventh one below that has the YES button on it.
Get the "Confirm Save As" element, and from that FindFirst(Treescope_Descendants...) the button (UIA_ButtonControlTypeId) with the name (UIA_NamePropertyId ) "&Yes".
 
Upvote 0
I can find the Window, but not the button.
What have I missed please?

1658917088312.png



Just out of interest, what is the difference between Children and Descendants (TreeScope)?
 
Upvote 0
Your code looks correct. Try it with "Yes" instead of "&Yes".

I guessed UIA_ButtonControlTypeId from the Spy++ screenshot. Please post the Inspect properties of the Yes button and its CtrlNotifySink parent - need the Name and the ControlType.

Just out of interest, what is the difference between Children and Descendants (TreeScope)?
Children are just the immediate children of the specified element. Descendants are the children and all their descendents in the element tree. See TreeScope (uiautomationclient.h) - Win32 apps.
 
Upvote 0
"Yes" on it's own worked. Much appreciated.

I am really liking this UIAutomation. Can it completely replace the "old" subclassing that I was doing before with FindWindow and FindWindowex?
It seems to be more reliable and there is less guesswork involved.
 
Upvote 0

Forum statistics

Threads
1,215,350
Messages
6,124,439
Members
449,160
Latest member
nikijon

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