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
814 views
in Technique[技术] by (71.8m points)

internet explorer - VBA code to interact with specific IE window that is already open

I am having trouble finding out how to get excel VBA to interact with a specific IE Window that is already open.

Here my process:

I open IE, navigate to the website, enter my username and pw, click log in. A new IE window opens that I use to enter member numbers and search for membership info.

I am trying to create a VBA script that will interact with the IE window that opens after I successfully log in (to begin to automate some of the more mundane tasks). I have seen tons of tutorials online about how to have VBA open a new IE window, and then interact with it. However, I am really struggling to find info about how to select a specific already open IE window and interact with it.

I have tried this as a quick test to see if I could select the correct IE window as an object that I can interact with.

Sub getIE()
Dim sh As Object, oWin As Object, IE As Object

Set sh = CreateObject("Shell.Application")

For Each oWin In sh.Windows
    If TypeName(oWin.document) = "HTMLDocument" Then
        Set IE = oWin
        Exit For
    End If
Next

MsgBox IE.document.URL

End Sub

The message box appears and shows the URL for initial IE window, but I don't know how to get it to select the IE window that I typically use to actually get member info.

I took several technical classes years ago when I was in school, but has been a while since I have flexed any tech muscles. Any help would be greatly appreciated!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here's what I use. It counts the number of open instances of IE and then steps through each instance and determines the url and title of the web page. You can then compare the title or url against the title or url you are looking for. The following example compares titles using the "Like" function, but like I said you could compare urls if you like. Also, if you are looking for a title or url that doesn't change you can use "=" instead of "Like".

marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    ' sometimes more web pages are counted than are open
    my_url = objShell.Windows(x).Document.Location
    my_title = objShell.Windows(x).Document.Title

    If my_title Like "XYZ" & "*" Then 'compare to find if the desired web page is already open
        Set ie = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next

If marker = 0 then
    msgbox("A matching webpage was NOT found")
Else
    msgbox("A matching webpage was found")
End If

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

...