Find correct tab in Internet Explorer and activate it

  • Hi,


    I need to find a specific tab in an open instance in Internet Explorer. I found a macro to bring Internet Explorer to the front, but then I need to find a specific tab and then activate it.
    I have found macros that can verify that the tab is open, but I haven't managed to merge any of those codes into the macro I have for bringing IE to the front.


    This is the macro I have for bringing IE to the front:
    (Found at "Happy Codings" site)




    I have seen some codes that uses SendKeys, but I would prefer to avoid that. And instead somehow loop through the names of the tabs to locate the correct one.


    I have also posted the question some time ago here:
    http://www.vbaexpress.com/foru…ance-of-Internet-Explorer



    Thank you,
    Bundi

  • Re: Find correct tab in Internet Explorer and activate it


    Try this code, which uses the CUIAutomation class to activate the required IE tab. This works on IE9 and higher.


    You must set the references noted at the top of the code: Tools -> References in the VBA editor.


  • Re: Find correct tab in Internet Explorer and activate it


    Thank you John for the code.


    I tried it in my intended application, but I get "Run Time Error '91': Object variable or With block variable not set " and it points to this line

    Code
    1. Set IEtabPattern = IEtab.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)


    This I got after changing the IE tab name (ozgrid) to the page I need in this line

    Code
    1. Set IE = Find_and_Activate_IE_Tab("*ozgrid*")


    When trying to run it without changing the IE tab name, I get this error "Run-time error '-2147467259 (80004005)'" Automation error Unspecified error.


    And when doing debug, it points to this line

    Code
    1. If TypeName(IE.document) = "HTMLDocument" Then


    Not sure really what is causing it.
    Thanks for your help

  • With IE11 you need to change the call to


    Code
    1. Dim Tabname
    2. Tabname= IE.Document.Title
    3. If Tabname= "" Then Tabname= IE.Document.Location.hostname
    4. Call UIAutomation_Click_IE_Tab(IE.hwnd, Tabname)


    In all my tests with IE11 it finds the required tab, which can be specified as the tab's URL or LocationName, including wildcards if needed. Can you give examples of web sites or pages where your change is needed?

  • UPDATE


    Recent tests with the UIAutomation_Click_IE_Tab procedure in post #2 generated the error:


    Run-time error '91':
    Object variable or With block variable not set


    At the following line:


    Code
    1. Set IEtabPattern = IEtab.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)


    The IEtab variable is Nothing because the previous line didn't find the specified tab name.


    I discovered that sometimes, although the visible the tab name - shown when hovering over the tab - may be "xxxxx", the actual tab name according to UIAutomation is "xxxxx Tab Group 1". Therefore looking for a tab named "xxxxx" will fail.


    The solution is to call UIAutomation_Click_IE_Tab like this:


    Code
    1. UIAutomation_Click_IE_Tab IE.hwnd, IE.LocationName & " Tab Group 1"


    Alternatively, here is a new procedure which uses the Like operator, so wildcards can be used (see https://docs.microsoft.com/en-us/off.../like-operator) to specify the tab name to be found and activated.


    For example, call it like this:

    Code
    1. UIAutomation_Click_IE_Tab_Like IE.hwnd, IE.LocationName & "*"
  • In all my tests with IE11 it finds the required tab, which can be specified as the tab's URL or LocationName, including wildcards if needed. Can you give examples of web sites or pages where your change is needed?

    Hi John_w,


    I try to use your code, but want to select the IE TAB by searching for a LocationURL instead of LocationName.

    Unfortunately, I am not able to find the URL equivalent for this property: UIA_NamePropertyId


    Can you make a suggestions?

  • You have to call Find_and_Activate_IE_Tab, which looks for the tab by its (partial) LocationURL or LocationName by looping through Shell.Windows. If found, it then calls UIAutomation_Click_IE_Tab to activate the tab. Find_and_Activate_IE_Tab uses the Like operator to look for the matching LocationURL or LocationName string, allowing wildcards in the TitleOrURL argument.


    This works for me:

  • The error occurs at the yellow highlighted line because the IEtab object is Nothing. And IEtab is Nothing because there is no tab (a TabItemControl) with the name of your URL. The tab name of each tab in IE's UIAutomation elements is the LocationName, not the LocationURL.


    Here is a detailed description of how the code works:


    The main function, Find_and_Activate_IE_Tab, looks for the required tab by its LocationName or LocationURL. If found, it calls UIAutomation_Click_IE_Tab with the found LocationName and looks for the IE UI element with the specified tab name (a TabItemControl) and clicks (activates) that tab.


    The 2nd argument to UIAutomation_Click_IE_Tab must be the LocationName of the tab to be activated, not the LocationURL. The reason is that, amongst all IE's UIAutomation elements, the UI element containing the LocationURL is that of the already active tab in IE's address bar - it is a UI element with the class name "AddressDisplay Control". Although the URL of a tab is displayed when you hover over that tab, I have not seen this URL anywhere in IE's UIAutomation elements, so I don't think the code could be changed to look for the URL.

  • Hi John,


    thank you again for your effort to explain this for me.


    Now I understand.


    I am trying to select one tab from many where, unfortunatelly, the LocationName is the same. The tabs contain pages for different records of a database so only id differes in the LocationURL. This is all I got to work with.


    So, I guess this is the end of my search for a solution to this.


    BR!

  • Here is a new UIAutomation function, UIAutomation_Click_IE_Tab_By_URL, which activates the tab with the specified LocationURL. It finds the required tab by activating each tab in turn and extracting the URL from the IE address bar and comparing it to the required URL - which can contain wildcards, since the Like operator is used - until the required tab is activated.


    Here is the complete code, including a test routine and a function which loops through all IE windows and tabs until the required LocationURL is found.


  • Hello,


    This code works very well for locating and activating a tab based on URL. Is there a similar method to activate a tab based on the page title and make that IE window visible? I don't need it to match the URL, only the page title.

  • royUK

    Closed the thread.