VBA-寻址Internet Explorer选项卡 [英] VBA - Addressing Internet Explorer tabs

查看:288
本文介绍了VBA-寻址Internet Explorer选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的是,我没有找到关于该主题的任何信息,并且我目前停留在设法通过编程方式单击按钮来在IE实例中打开新选项卡的那一刻,但是我还不是最笨拙的人.如何寻址新选项卡以便从那里获取信息的线索(该按钮基本上会显示一个带有搜索结果的新选项卡).

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).

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

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

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

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") ?

奖金问题:由于我是VBA和Internet Explorer交互的新手,因此变量到底是什么:htmlButton, allTags, Tag?另外,有人可以解释我是否需要将所有变量最后都设置为空,还是只需要将Internet Explorer设置为空?

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?

提前谢谢!

推荐答案

请参见下文,获取可用于获取打开的IE文档窗口的功能-我认为IE不会公开任何简单的(可通过VBA访问的)API来进行工作直接使用标签或确定特定标签是否处于活动状态.

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.

allTags是类型为"的DOM元素的集合,而Tag是该集合的单个成员.

allTags is a collection of DOM elements with type "" , and Tag is a single memeber of that collection.

在退出Sub之前,您不必将对象设置为Nothing(尽管有些人仍然这样做)-VBA运行时将为您解决这一问题.

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

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

这篇关于VBA-寻址Internet Explorer选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆