Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

internet explorer - Get IE window object by window title with VBA

I found this solution provided by @mkingston: How to intercept and manipulate a Internet Explorer popup with VBA

...but it's not working for me. I've added both of the reference libraries in question, but when I run the script I run into these issues:

Compile error: Undefined Sub due to pauseUntilIEReady (since this Sub wasn't included with the answer, I remove it from the script)

Compile error: Argument not optional due to oGetIEWindowFromTitle (so I tried commeting this out to get the script to compile)

After the script finally compiles, it get this error:

Automation error The system cannot find the file specified.

on this line of code: For Each oGetIEWindowFromTitle In objShellWindows

Here's the code I'm trying to run:

Function oGetIEWindowFromTitle(sTitle As String, _
                           Optional bCaseSensitive As Boolean = False, _
                           Optional bExact As Boolean = False) As SHDocVw.InternetExplorer

Dim objShellWindows As New SHDocVw.ShellWindows
Dim found As Boolean
Dim startTime As Single

found = False
'Loop through shell windows
For Each oGetIEWindowFromTitle In objShellWindows
    found = oGetIEWindowFromTitleHandler(oGetIEWindowFromTitle, sTitle, bCaseSensitive, bExact)
    If found Then Exit For
Next

'Check whether a window was found
If Not found Then
    Set oGetIEWindowFromTitle = Nothing
Else
    'COMMENTED OUT TO GET SCRIPT TO COMPILE pauseUntilIEReady oGetIEWindowFromTitle
End If

End Function


Private Function oGetIEWindowFromTitleHandler(win As SHDocVw.InternetExplorer, _
                                  sTitle As String, _
                                  bCaseSensitive As Boolean, _
                                  bExact As Boolean) As Boolean

oGetIEWindowFromTitleHandler = False

On Error GoTo handler
'If the document is of type HTMLDocument, it is an IE window
If TypeName(win.Document) = "HTMLDocument" Then
    'Check whether the title contains the passed title
    If bExact Then
        If (win.Document.title = sTitle) Or ((Not bCaseSensitive) And (LCase(sTitle) = LCase(win.Document.title))) Then oGetIEWindowFromTitleHandler = True
    Else
        If InStr(1, win.Document.title, sTitle) Or ((Not bCaseSensitive) And (InStr(1, LCase(win.Document.title), LCase(sTitle), vbTextCompare) <> 0)) Then oGetIEWindowFromTitleHandler = True
    End If
End If
handler:
'We assume here that if an error is raised it's because
'the window is not of the correct type. Therefore we
'simply ignore it and carry on.

End Function

and

Sub test()

Dim ie As SHDocVw.InternetExplorer
Dim doc As HTMLDocument 'If you have a reference to the HTML Object Library
'Dim doc as Object 'If you do not have a reference to the HTML Object Library

' Change the title here as required
Set ie = oGetIEWindowFromTitle("My popup window")
Set doc = ie.Document

Debug.Print doc.getElementsByTagName("body").Item(0).innerText

End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works for me.

Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer

    Dim objShellWindows As New SHDocVw.ShellWindows
    Dim win As Object, rv As SHDocVw.InternetExplorer

    For Each win In objShellWindows
        If TypeName(win.Document) = "HTMLDocument" Then
            If UCase(win.Document.Title) = UCase(sTitle) Then
                Set rv = win
                Exit For
            End If
        End If
    Next

    Set IEWindowFromTitle = rv

End Function

Sub Tester()

    Dim w As SHDocVw.InternetExplorer
    Set w = IEWindowFromTitle("Google")
    If Not w Is Nothing Then
        Debug.Print w.Document.Title
    Else
        Debug.Print "Not found"
    End If

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...