vba 创建后访问 IE 选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16523271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:04:40  来源:igfitidea点击:

Accessing IE tabs once created

internet-explorervbainternet-explorer-10

提问by user3357963

Using VBA I can create an InternetExplorer object with 3 different tabs using the following

使用 VBA,我可以使用以下命令创建具有 3 个不同选项卡的 InternetExplorer 对象

Option Explicit

Public Enum IE_READYSTATE
    Uninitialised = 0
    Loading = 1
    Loaded = 2
    Interactive = 3
    complete = 4
End Enum

Sub Example_Click()
Dim ieApp As clsIE

    'Create IE and login
    If ieApp Is Nothing Then
        Set ieApp = New clsIE
        With ieApp

            'IE Tab1
            .IE.Visible = True
            .IE.navigate "http://www.bbc.co.uk/news/"
            Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop

            'IE Tab2
            .IE.Navigate2 "http://www.bbc.co.uk", CLng(2048)
            Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop

            'IE Tab3
            .IE.Navigate2 "http://www.bbc.co.uk", CLng(2048)
            Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop


        End With
    End If

End Sub

How can I then access these tabs to....

我如何才能访问这些选项卡以....

  1. Quit/Close a specific tab?
  2. Navigate to a new URL on a specific tab?
  3. Access a specific tab DOM?
  1. 退出/关闭特定选项卡?
  2. 导航到特定选项卡上的新 URL?
  3. 访问特定的标签 DOM?

I know how to do all of this with a single tab but not multiple tabs?

我知道如何使用单个选项卡而不是多个选项卡来完成所有这些操作?

采纳答案by Graham Anderson

I have found an answer that appears to work, though I haven't done rigorous testing. I've modified your code so that I can run it without your clsIE module. This code navigates the tabs to 3 urls when opening them and then navigates them to new urls using the shellwindows object.

我找到了一个似乎有效的答案,尽管我没有做过严格的测试。我已经修改了你的代码,这样我就可以在没有你的 clsIE 模块的情况下运行它。此代码在打开选项卡时将它们导航到 3 个 url,然后使用 shellwindows 对象将它们导航到新的 url。

I changed the do while .busy.. lines as they didn't work for the 2nd and 3rd tabs in that the state of the IE applecation was ready whilst the new tabs were still loading.

我更改了 do while .busy.. 行,因为它们不适用于第二个和第三个选项卡,因为 IE 应用程序的状态已准备就绪,而新选项卡仍在加载。

Sub Example_Click()
Dim ieApp As InternetExplorer
Dim SWs As ShellWindows
Dim IETab1Number As Integer
Dim IETab2Number  As Integer
Dim IETab3Number As Integer

Set SWs = New ShellWindows

'Create IE and login
If ieApp Is Nothing Then
    Set ieApp = CreateObject("InternetExplorer.Application")
    With ieApp

       'IE Tab1
       .Visible = True
       .Navigate "http://www.bbc.co.uk/news/"
       Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop
       IETab1Number = SWs.Count

       'IE Tab2
       .Navigate2 "http://www.bbc.co.uk/news/uk-scotland-north-east-orkney-shetland-23822420", CLng(2048)
       'Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop
       Do While SWs.Count = IETab1Number: DoEvents: Loop

       IETab2Number = SWs.Count

       'IE Tab3
       .Navigate2 "http://www.bbc.co.uk", CLng(2048)
       'Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop
       Do While SWs.Count = IETab2Number: DoEvents: Loop
       IETab3Number = SWs.Count
       'ieApp.Visible = False

       SWs.Item(IETab1Number - 1).Navigate "http://www.bbc.co.uk/"
       SWs.Item(IETab2Number - 1).Navigate2 "http://www.bbc.co.uk/news/"
       SWs.Item(IETab3Number - 1).Navigate2 "http://www.bbc.co.uk/news/"


    End With
End If
Set ieApp = Nothing
Set SWs = Nothing

End Sub

It uses the ShellWindows to manipulate the tabs. This is only done by number so I'm guessing it could be prone to errors.

它使用 ShellWindows 来操作选项卡。这只能通过数字完成,所以我猜它可能容易出错。

To make it more robust you may want to get some info from the tab after an operation and check that the value is the same when returning to it. For example sPageTitle = SWs.Item(IETab3Number - 1).Document.Titlecould be used to store the title of the page in a tab, the next time you want to use the tab you can check that it hasn't changed.

为了使其更健壮,您可能希望在操作后从选项卡中获取一些信息,并在返回时检查该值是否相同。例如sPageTitle = SWs.Item(IETab3Number - 1).Document.Title,可用于将页面标题存储在选项卡中,下次您想使用该选项卡时,您可以检查它是否未更改。

回答by Neechalkaran

Simple workaround is just reassign the new tab to the object

简单的解决方法是将新选项卡重新分配给对象

IE.navigate "http://www.bbc.co.uk/news/", CLng(2048)
Do While IE.Busy Or Not IE.readyState = IE_READYSTATE.complete: DoEvents: Loop
With CreateObject("Shell.Application").Windows
Set IE = .Item(.Count - 1)
End With

回答by Winand

Here is how I solved the problem of grouping tabs in one IE window using VBScript. If the function finds IE window with a tab whose URL starts with tabGroupS(e.g. "http://stackoverflow.com") then it adds a new tab to this window and returns this tab otherwise it opens a new window.

下面是我如何使用 VBScript 解决在一个 IE 窗口中分组选项卡的问题。如果该函数找到带有 URL 开头的选项卡的 IE 窗口tabGroupS(例如“ http://stackoverflow.com”),则它会向该窗口添加一个新选项卡并返回该选项卡,否则会打开一个新窗口。

Private Function getNewTab(tabGroupS)
    Dim i, objs, cnt, openflag
    Set objs = CreateObject("Scripting.Dictionary")
    Set SWObj = CreateObject("Shell.Application").Windows()
    For Each i In SWObj
        If LCase(Right(i.FullName, 12)) = "iexplore.exe" Then objs.Add objs.Count, i
    Next
    cnt = SWObj.Count
    For Each i In objs
        If Left(objs(i).LocationURL, Len(tabGroupS)) = tabGroupS Then
            openflag = True
            objs(i).Navigate2 "about:blank", &H800
            Exit For
        End If
    Next
    If Not openflag Then
        Set getNewTab = CreateObject("InternetExplorer.Application")
        getNewTab.Visible = True
        Exit Function
    End If
    Do: WScript.Sleep 100: Loop Until SWObj.Count > cnt 'wait until new tab is opened
    For Each i In SWObj
        If LCase(Right(i.FullName, 12)) = "iexplore.exe" Then
            If Not hasObj(i, objs) Then
                Set getNewTab = i
                Exit Function
            End If
        End If
    Next
End Function

Function hasObj(obj, col)
    For Each i In col
        If obj Is col(i) Then
            hasObj = True
            Exit Function
        End If
    Next
End Function

回答by Raulsoul

Try this code:

试试这个代码:

Sub FillinternetForm()
    Dim ie As Object
    Set ie = CreateObject("Internetexplorer.Application")
    ie.Navigate "https://google.com"
    ie.Visible = True
    While ie.Busy
        DoEvents 'wait until IE is done loading page.
    Wend
    ie.Document.all("lst-ib").Value = "Fast Sub"
    ie.Navigate "https://google.com", CLng(2048)
    ie.Document.all("lst-ib").Value = "Fast slow Sub"
End Sub