VBA - 寻址 Internet Explorer 选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27541862/
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-12 05:33:44  来源:igfitidea点击:

VBA - Addressing Internet Explorer tabs

vbainternet-explorertabs

提问by Noldor130884

Strangely enough I didn't find any information on the topic and I'm currently stuck at the point where I managed to open a new tab in an instance of IE by programmatically clicking a button, but I haven't the faintest clue of how to address the new tab in order to get information from there (the button basically brings up a new tab with the result of a search).

奇怪的是,我没有找到有关该主题的任何信息,而且我目前陷入了通过以编程方式单击按钮而设法在 IE 实例中打开一个新选项卡的地方,但我对此一无所知寻址新选项卡以便从那里获取信息(该按钮基本上会显示一个带有搜索结果的新选项卡)。

This is basically a straightforward question, but I'm including my code anyway:

这基本上是一个简单的问题,但无论如何我都包括我的代码:

Sub AddInfoFromIntranet()

Dim Ie As SHDocVw.InternetExplorer
Dim URL As String
Dim iFrames As MSHTML.IHTMLElementCollection
Dim iFrame As MSHTML.HTMLFrameElement
Dim Doc As MSHTML.HTMLDocument
Dim InputBox As MSHTML.IHTMLElementCollection, htmlButton, allTags, Tag

' Opens Intranet - yeah, sadly it's not a public web page
URL = "{My intranet website}"
Set Ie = New SHDocVw.InternetExplorer
With Ie
    .navigate URL
    .Visible = True
    While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    Set Doc = .document
End With

' Gets top_window frame and navigates to it, then inserts the name to search
Set iFrames = Doc.getElementsByName("top_window")
If Not iFrames Is Nothing Then

    Set iFrame = iFrames(0)
    Ie.navigate URL & iFrame.src

    While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    Set InputBox = Doc.getElementsByName("Nachnamevalue")
    If Not InputBox Is Nothing Then InputBox(0).Value = "test"

    ' Clicks on "search"
    Set allTags = Doc.getElementsByTagName("input")
    For Each Tag In allTags
        If Tag.Value = "suchen" Then 
            Tag.Click
            Exit For
        End If
    Next

    ' Here a new tab is opened, must find info in this tab
    While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend

    ' HERE I HAVE NO CLUE WHAT TO WRITE. THE CODE ABOVE WORKS FLAWLESSLY

End If

Set Doc = Nothing
Set iFrames = Nothing
Set iFrame = Nothing
Set InputBox = Nothing
Set allTags = Nothing
Set Ie = Nothing

Ie.Quit

End Sub

Now, is there a way to address a tab by: 1) its name (and where do I find it) 2) its position in browser 3) the status (if it is "active") ?

现在,有没有办法通过以下方式来寻址选项卡:1)它的名称(以及我在哪里找到它)2)它在浏览器中的位置 3)状态(如果它是“活动的”)?

Bonus questions: since I am new to VBA and Internet Explorer interaction, what exactly are the variables: htmlButton, allTags, Tag? Also, could anyone explain if I need to set all the variables at the end to nothing, or I just need to set the Internet Explorer to nothing?

额外问题:由于我是 VBA 和 Internet Explorer 交互的新手,变量到底是什么:htmlButton, allTags, Tag?另外,谁能解释一下我是否需要在最后将所有变量设置为空,或者我只需要将 Internet Explorer 设置为空?

Thanks in advance!

提前致谢!

回答by Tim Williams

See below for a function you can use to get an open IE document window - I don't think IE exposes any simple (VBA-accessible) API for working directly with tabs or determining whether a specific tab is active.

请参阅下面的功能,您可以使用它来获取打开的 IE 文档窗口 - 我认为 IE 没有公开任何简单的(VBA 可访问的)API 来直接使用选项卡或确定特定选项卡是否处于活动状态。

allTagsis a collection of DOM elements with type "" , and Tagis a single memeber of that collection.

allTags是类型为 "" 的 DOM 元素的集合,并且Tag是该集合的单个成员。

You do not haveto set objects to Nothing before exiting a Sub (though some people still do that) - the VBA runtime will take care of that for you.

你不具备的VBA运行时会照顾的,对于你-退出子(尽管有些人仍然这样做)之前,设置对象为Nothing。

Sub TestGetIE()
Dim IE As Object

    Set IE = GetIE("http://stackoverflow.com")
    If Not IE Is Nothing Then
        IE.document.execCommand "Print", False, 0
    End If

End Sub

'Get a reference to an open IE window based on its URL
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
        'check the URL and if it's the one you want then
        ' assign it to the return value
        sURL = o.document.Location
        On Error GoTo 0
        'Debug.Print sURL
        If sURL Like sLocation & "*" Then
            Set retVal = o
            Exit For
        End If
    Next o

Set GetIE = retVal

End Function