VBA: 在 Internet Explorer 上选择特定选项卡

VBA: Choosing Specific Tab on Internet Explorer

我有另一个 post 关于同一个整体问题的不同问题:。我的编码现在遇到了一个新问题(我将 post 下面的一部分),如果我打开带有多个选项卡的 Internet Explorer,我的代码将不再填充文本框 - 即使该选项卡是当前正在查看的选项卡。一旦我关闭所有其他选项卡,代码就会完美运行。

如果选项卡在 URL 中被命名为 Tab1https://sub.website.com/dir/,我如何使用多个选项卡填写此网站上的表格?

这是正在使用的代码 (由 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

使用 Shell Windows 循环并保留找到的第一个。关闭剩余的 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

要使用选项卡名称进行标识,这将有效:

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

这是我在自动化现有 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

Dim ieApp As New SHDocVw.InternetExplorer    'Microsoft Internet Controls' Reference 
Dim SWs As New ShellWindows                  'Objects for Multiple Tabs

ieApp.Visible = True
ieApp.Navigate "Https://duckduckgo.com/"
ieApp.Navigate "https://in.yahoo.com/", CLng(2048)   'Open URL in New Tab

''''' Wait for the page to finish loading '''''

Debug.Pritnt SWs.Count              'Total Tabs + 1
Set ieApp = SWs.Item(1)             'Assiging Tabs[index]
Debug.Print ieApp.LocationURL       'Display the Current URL
Debug.Print ieApp.LocationName      'Display the Current Tab Name

Set ieApp = SWs.Item(2)             'Assiging Tabs[index]
Debug.Print ieApp.LocationURL       'Display the Current URL
Debug.Print ieApp.LocationName      'Display the Current Tab Name