Get control on newly opened tab on webpage

Dyak

New Member
Joined
Aug 7, 2020
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
My code opens up a webpage and then click on a certain link on that page which opens in a new tab. I am hoping to get "control" on that new tab (using getelemenbyid, etc), but alas, my code remains on referring to my first page.

Sorry, for some reason I cant share the code and I have very little knowledge in web automation.

Any help would be greatly appreciated. Thanks!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Welcome to MrExcel forums.

You can loop through Shell.Windows, which contains all the IE tabs and windows and File Explorer windows, looking for the URL or location name of the required tab. Call this function:

VBA Code:
Private Function Get_IE_Window(URLorName As String) As InternetExplorer

    'Look for an IE browser window or tab already open at the (partial) URL or location name and, if found, return
    'that browser as an InternetExplorer object.  Otherwise return Nothing

    Dim Shell As Object
    Dim IE As InternetExplorer
    Dim i As Variant 'Must be a Variant to index Shell.Windows.Item() array
   
    Set Shell = CreateObject("Shell.Application")
   
    i = 0
    Set Get_IE_Window = Nothing
    While i < Shell.Windows.Count And Get_IE_Window Is Nothing
        Set IE = Shell.Windows.Item(i)
        If Not IE Is Nothing Then
            Debug.Print IE.LocationURL, IE.LocationName
            If IE.Name = "Internet Explorer" And InStr(IE.LocationURL, "file://") <> 1 Then
                If InStr(1, IE.LocationURL, URLorName, vbTextCompare) > 0 Or InStr(1, IE.LocationName, URLorName, vbTextCompare) > 0 Then
                    Set Get_IE_Window = IE
                End If
            End If
        End If
        i = i + 1
    Wend
   
End Function
Like this:
VBA Code:
    Dim IE As InternetExplorer

    Set IE = Get_IE_Window("URL or location name of required tab, or part thereof")
    If IE Is Nothing Then
        MsgBox "Tab not found"
    Else
        MsgBox "Found tab " & IE.LocationURL & " - " & IE.LocationName
    End If
The code requires a reference to MS Internet Controls.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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