VBA: Choosing Specific Tab on Internet Explorer

耗尽温柔 提交于 2019-11-28 06:53:37

问题


I had another post regarding a different issue on the same overall problem here: Converting From Early Binding to Late Binding. I now have a new issue with my coding (which I will post a portion of it below), where if I have Internet Explorer open with multiple tabs, my code no longer fills in the text boxes - even if the tab is the one currently being viewed. As soon as I close all other tabs, the code runs flawlessly.

If the tab was named Tab1 at the URL: https://sub.website.com/dir/, how can I have the forms filled out on this site with multiple tabs?

Here is the code being used (courtesy of cyboashu and help from Tim Williams):

Sub Test()
 ' Code Cut Here
    Dim oShell      As Object
    Dim oWin        As Object
    Dim IE          As Object
    Dim lTotlWin    As Long
    Dim lCtr

    Debug.Print Time & " --- IE Objects & Values ---"       ' Debugger Section
    Set oShell = CreateObject("Shell.Application")
        Debug.Print Time & " [obj ] oShell..: " & oShell    ' Debug oShell
    Set oWin = oShell.Windows()
        Debug.Print Time & " [obj ] oWin....: " & oWin      ' Debug oWin

    lTotlWin = oWin.Count - 1   '/ Starts with zero
    Debug.Print Time & " [long] lTotlWin: " & lTotlWin      ' Debug lTotlWin

    For lCtr = 0 To lTotlWin
        If UCase(oWin.Item(lCtr).FullName) Like "*IEXPLORE.EXE" Then
            Set IE = oWin.Item(lCtr)
        End If
    Next
    Debug.Print Time & " [obj ] IE......: " & IE
    If Not IE Is Nothing Then
        MsgBox "Found and hooked!!"
    End If

    Dim TBox As String
    Dim TBtn As String
        TBox = "masked1"
        Tbtn = "button"


    If Not IE Is Nothing Then
        Set txtBox = IE.Document.getElementsByClassName(TBox)(0)
        Debug.Print Time & " [obj ] txtbox..: " & txtbox
        Set submitBtn = IE.Document.getElementsByClassName(Tbtn)(4)
        Debug.Print Time & " [obj ] submitBtn:" & submitBtn

        txtBox.Value = tVal
        submitBtn.Click
    End If
End Sub

回答1:


Here's what i typically use when automating an existing IE window:

Sub Tester()
     Dim IE As Object
     Set IE = GetIE("http://www.google.com")
     Debug.Print IE.document.Title
     'work with IE
End Sub


Function GetIE(sLocation As String) As Object

    Dim objShell As Object, objShellWindows As Object, o As Object
    Dim sURL As String
    Dim retVal As Object

    Set retVal = Nothing
    Set objShell = CreateObject("Shell.Application")
    Set objShellWindows = objShell.Windows

    For Each o In objShellWindows
        sURL = ""
        On Error Resume Next  'because may not have a "document" property
        'Check the URL and if it's the one you want then
        ' assign the window object to the return value and exit the loop
        sURL = o.document.Location
        On Error GoTo 0
        If sURL Like sLocation & "*" Then
            Set retVal = o
            Exit For
        End If
    Next o

    Set GetIE = retVal

End Function



回答2:


Use the Shell Windows to loop through and keep the first one that you found. Closing remaining IE tabs/windows.

Sub testTabClose()

    Dim oShell              As Object
    Dim oWin                As Object
    Dim IE                  As Object
    Dim lTotlWin            As Long
    Dim lCtr
    Dim lCtr2


    Set oShell = CreateObject("Shell.Application")
    Set oWin = oShell.Windows()

    lTotlWin = oWin.Count - 1 '/ Starts with zero

    For lCtr = 0 To lTotlWin
        If UCase(oWin.Item(lCtr).FullName) Like "*IEXPLORE.EXE" Then
            Set IE = oWin.Item(lCtr)
            '/ Found it, jump out.
            Exit For
        End If

    Next

    '/ Keep the first tab. Close rest of them
    For lCtr2 = lCtr + 1 To oWin.Count - 1
        If lCtr2 <> lCtr Then
         oWin.Item(lCtr2).Quit '/ Kill the tab.
        End If
    Next

End Sub

To use tab names for identification this will work :

Set oShell = CreateObject("Shell.Application")
    Set oWin = oShell.Windows()

    lTotlWin = oWin.Count - 1 '/ Starts with zero

    For lCtr = 0 To lTotlWin
        If lTotlWin >= lCtr Then
            If UCase(oWin.Item(lCtr).FullName) Like "*IEXPLORE.EXE" Then
                If IE Is Nothing Then
                    Set IE = oWin.Item(lCtr)
                End If
                If oWin.Item(lCtr).document.Title <> "This is the title I am looking for." Then
                        '/ This tab is not needed.
                        oWin.Item(lCtr).Quit
                End If
            End If
        End If
    Next


来源:https://stackoverflow.com/questions/38859727/vba-choosing-specific-tab-on-internet-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!