AnDanDan
AnDanDan

Reputation: 47

Create Internet Explorer variable from shell.application object

I'm retrieving an active internet explorer tab to run an automation script for an authenticated site. I need to get the application instance itself as something to operate on, as I have a way to navigate to the page I want since the button has no ID to call it by.

I start by finding the right page here, and then from there things are smooth.

Dim oShell, oWSHShell, sTitle, wndw, bMatch, oSelect
set oShell =createobject("shell.application")
set oWSHShell = createobject("wscript.shell")
sHTMLTitle = "Add Account"

bMatch =false 
for each wndw in oShell.windows 
    if instr(lcase(typename(wndw.document)), "htmldocument") > 0 then 
        sTitle =wndw.document.title
        if Instr(sTitle, sHTMLTitle)<> 0  then 
            bMatch =true 
            exit for 
        end if 
    end if 
next 

Is there a way I can save the oShell object as something I can reference later on? Any reference I have seen for declaring an IE object is for making a brand new window which won't work here, as you would have to re-authenticate yourself and defeat the whole purpose.

Upvotes: 1

Views: 1261

Answers (1)

Bond
Bond

Reputation: 16321

If I understand you correctly then I think you were on the right track, based on your comment. wndw is your IE object. But you need to use the Set keyword when assigning an object reference. For example:

Set objSaveIE = Nothing

for each wndw in oShell.windows 
    if instr(lcase(typename(wndw.document)), "htmldocument") > 0 then 
        sTitle =wndw.document.title
        if Instr(sTitle, sHTMLTitle)<> 0  then 
            Set objSaveIE = wndw 
            exit for 
        end if 
    end if 
next

' Now you can use objSaveIE as you would any other IE object...
If Not objSaveIE Is Nothing Then objSaveIE.Navigate "www.google.com"

Upvotes: 2

Related Questions