How to navigate to an already opened internet explorer window? (VBA)

后端 未结 2 2082
感动是毒
感动是毒 2021-01-28 16:54

so I\'ve been searching around the web a lot on how to do this and can\'t seem to find any concrete information on this. I\'ve seen a lot of examples on how to open a new intern

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-28 17:31

    I try to check your description and I find that you want to get object of already opened IE window and than try to automate it.

    You can try to refer example below may help you.

    In this example, You can see that already opened page get searched using it's title. Than you can use it's object to automate that page.

    Sub demo()
    
    Dim str_val As Object
    marker = 0
    Set objShell = CreateObject("Shell.Application")
    IE_count = objShell.Windows.Count
    For x = 0 To (IE_count - 1)
        On Error Resume Next
        my_url = objShell.Windows(x).document.Location
        my_title = objShell.Windows(x).document.Title
    
        If my_title Like "XYZ" & "*" Then
            Set IE = objShell.Windows(x)
            marker = 1
            Exit For
        Else
        End If
    Next
    
    If marker = 0 Then
     MsgBox ("A matching webpage was NOT found")
    
    Else
        MsgBox ("A matching webpage was found")
    
        Set str_val = IE.document.getElementById("txtbox1")
        str_val.Value = "demo text"
    
    End If
    End Sub
    

    Output:

    Reference:

    VBA code to interact with specific IE window that is already open

提交回复
热议问题