Home > Mobile >  Target existing IE window tab using VBA
Target existing IE window tab using VBA

Time:09-18

I'm trying to target an existing IE window (one tab only) with my script.

In other words: there's an Internet Explorer window opened with a specific tab and I'm trying to get my script to target that tab using its tab name. I had a look at these topics, but none worked for me (or I did it wrong):

  • Error 438 - Object doesn't support this property or method

    Object doesn't support this property or method (Error 438)

    The error highlights the following line:

    WindowTitle = Application.Document.Title
    

    Having read the Microsoft Doc related to the error, I tried changing the WindowTitle variable to an object. Didn't work.

    Does anyone have an idea?

    Thanks!

    CodePudding user response:

    This works for me - finds IE by looking for an open window with the passed URL:

    Sub tester()
        Dim w As Object
        
        Set w = FindingExistingIEByUrl("https://www.google.com")
        If Not w Is Nothing Then
            MsgBox w.document.Title
        Else
            MsgBox "No IE window found"
        End If
    End Sub
    
    
    Function FindingExistingIEByUrl(theUrl)
        Dim w As Object
        For Each w In CreateObject("Shell.Application").Windows
            'Debug.Print w.locationurl
            If w.locationurl Like theUrl & "*" Then
                Set FindingExistingIEByUrl= w
                Exit Function
            End If
        Next w
    End Function
    
    
    Function FindingExistingIEByTitle(theTitle)
        Dim w As Object, t As String
        For Each w In CreateObject("Shell.Application").Windows
            t = ""
            On Error Resume Next 'ignore error if no Document
            t = w.Document.Title
            On Error Goto 0
            If t Like theTitle Then
                Set FindingExistingIEByTitle = w
                Exit Function
            End If
        Next w
    End Function
    
  • Related