简体   繁体   中英

VBA - Addressing Internet Explorer tabs

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

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

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?

Thanks in advance!

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 is a collection of DOM elements with type "" , and Tag is a single memeber of that collection.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM