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

前端 未结 2 367
温柔的废话
温柔的废话 2020-12-06 03:38

I am having trouble finding out how to get excel VBA to interact with a specific IE Window that is already open.

Here my process:

I open IE, navigate to the

相关标签:
2条回答
  • 2020-12-06 03:57

    The above code worked well for me. I turned it into a function call as follows

    Set ieFindUser = FindIEObject("Find and List Users")            'Find handle for popup window.
    

    The Function

    Public Function FindIEObject(target As String) As InternetExplorerMedium
        Set objShell = CreateObject("Shell.Application")
        IE_count = objShell.Windows.Count
        For x = 0 To (IE_count - 1)
            On Error Resume Next    ' sometimes more web pages are counted than are open
            my_url = objShell.Windows(x).Document.Location
            my_title = objShell.Windows(x).Document.Title
            If my_title = target Then 'compare to find if the desired web page is already open
                Set FindIEObject = objShell.Windows(x)
                Exit For
            End If
        Next
    End Function
    
    0 讨论(0)
  • 2020-12-06 04:01

    Here's what I use. It counts the number of open instances of IE and then steps through each instance and determines the url and title of the web page. You can then compare the title or url against the title or url you are looking for. The following example compares titles using the "Like" function, but like I said you could compare urls if you like. Also, if you are looking for a title or url that doesn't change you can use "=" instead of "Like".

    marker = 0
    Set objShell = CreateObject("Shell.Application")
    IE_count = objShell.Windows.Count
    For x = 0 To (IE_count - 1)
        On Error Resume Next    ' sometimes more web pages are counted than are open
        my_url = objShell.Windows(x).Document.Location
        my_title = objShell.Windows(x).Document.Title
    
        If my_title Like "XYZ" & "*" Then 'compare to find if the desired web page is already open
            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")
    End If
    
    0 讨论(0)
提交回复
热议问题