Home > front end >  VBA objShell.windows.title not finding title
VBA objShell.windows.title not finding title

Time:01-30

I run this function with two IE browsers open. IE_count finds six objects, but it does not find any titles (my_title) within the for loop for objShell. They all return an empty string.

Any idea as to why this could be? Relevant code below:

' code below adapted from ron's answer here: https://stackoverflow.com/questions/21407340/how-to-read-text-from-an-already-open-webpage-using-vba
Function SecondBrowserSearchForAndClick(ElementID As String, searchFor As String)
    Dim objShell
    Set objShell = CreateObject("Shell.Application")
    
    Dim IE_count As Integer
    IE_count = objShell.Windows.Count
    
    Dim x As Integer
    For x = 0 To (IE_count - 1)
        On Error Resume Next    ' sometimes more web pages are counted than are open
        
        Dim my_url As String
        my_url = objShell.Windows(x).document.Location
        
        Dim my_title As String
        my_title = objShell.Windows(x).document.Title
        
        If my_title Like "*Select Process*" Then 'compare to find if the desired web page is already open
            Dim tagColl_TR As Object
            Set tagColl_TR = objShell.Windows(x).document.getElementById(ElementID).contentDocument.getElementsByTagName("tr")
            Dim f
            While f < tagColl_TR.Length
                If tagColl_TR(f).Children.Length = 5 Then
                    If tagColl_TR(f).Children(3).Children(0).innerText Like "*" & searchFor & "*" Then
                        tagColl_TR(f).Children(1).Children(0).Children(1).Focus
                        tagColl_TR(f).Children(1).Children(0).Children(1).Click
                        Exit Function
                    End If
                End If
                f = f   1
            Wend
        End If
    Next
End Function

Any help would be appreciated.

CodePudding user response:

It's easier to put the "find document by title" functionality in its own function:

Sub Tester()
    Dim doc As Object
    
    Set doc = IEDocumentByTitle("Google")
    If Not doc Is Nothing Then
        Debug.Print "Found window at: " & doc.Location
        'work on doc here
    End If
End Sub

'Return an open IE document based on its Title property
Function IEDocumentByTitle(title As String)
    Dim w As Object, ttl As String
    For Each w In CreateObject("Shell.Application").Windows
        If w.Application.Name = "Internet Explorer" Then 'filter out Windows Explorer
            ttl = ""
            On Error Resume Next
            ttl = w.document.title
            On Error GoTo 0
            If ttl Like title Then
                Set IEDocumentByTitle = w.document
                Exit Function
            End If
        End If
    Next w
End Function

This works fine for me.

BTW the shell Windows collection also includes Windows Explorer instances in addition to IE windows/tabs.

Also you should really cancel On Error Resume Next as soon as possible or it will silently swallow all errors in your code, possibly leading to unexpected results.

  •  Tags:  
  • Related