Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Controlling an already existing instance of IE

Status
Not open for further replies.

Elektryon

Technical User
Jul 10, 2004
12
US
It's me again with another question on this topic (this will be my 3rd post on it)

I have the this code (given by Tsuji), which is suppose to get a handle on an already existsing instance of IE (given its current URL), and it works quite well, however, it has this little quirk: If there's a folder window open, it will get the handle on that, rather than IE. It will work fine if there isn't a folder window open, even if there are many IE instances running. It's quite odd actually.

Code:
sURL="[URL unfurl="true"]http://www.google.com/"[/URL]
set oie=nothing
set shapp=createobject("shell.application")


on error resume next
for each owin in shapp.windows
   if strcomp(sURL,owin.document.location.href,1)=0 then
	wscript.echo owin.document.location.href
        set oie=owin
   end if
next
on error goto 0


set owin=nothing : set shapp=nothing

if not oie is nothing then
    'here you have got the searched object
    'and can manipulate it, for instance navigate.
    oie.navigate "[URL unfurl="true"]www.microsoft.com"[/URL]
end if
set oie=nothing

Thanks for the read.
 
Hello Elektryon,

I have not answered back earlier because I want somebody to give you an independent line of suggestion. But, since nobody voluntees yet, I will give it a go.

For a folder window, you'll see owin.document err out already---that's why error trapping is implemented there not only to identify the url is what wanted, it also filters out "folder" window. So I would be surprised if you captured a "folder" window with oie.

regards - tsuji
 
Funnily enough, just seconds after I read your post did I figure out why it would get the handle on a folder window.

Consider the following:

There are 4 windows open, 3 IE browsers and 1 folder wind, and shapp.windows contains, in this order:

1. Microsoft Internet Explorer
2. Microsoft Internet Explorer
3. (folder window)
4. Microsoft Internet Explorer

We want to get the handle on the 4th IE window (it's the one on google), so, the script will go to the first one, test its url, it's not it, goes to the second one, that's not it either, but then it gets to the 3rd window in shapp.windows, the folder window. The script tests "if strcomp(sURL,owin.document.location.href,1)=0 then", but the folder window does not have a .document.location.href , so, it results in an error, and on error resume next is on, so it goes to the next statement, the set oie=owin, and oie is still set to the folder window. Therefore, what has happened here is that the error trapping set up to avoid getting the handle on what we don't want, ends up getting the handle on exactly what we don't want by skipping the If statement.

So now what is needed is some sort of property or method that is univeral to all windows in shapp.windows that would also let us distinguish between the types of windows. Once the script knows that what it's looking at is an actual IE window, then it can test for the href property, without error. Or something in to that effect.
 
Have you tried something like this ?
For Each owin In shapp.Windows
If StrComp(sURL,owin.Document.Location.Href,1)=0 Then
If Err.Number = 0 Then
WScript.Echo owin.Document.Location.Href
Set oie=owin
Exit For
Else
Err.Clear
End If
End If
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Elecktryon & PHV,

So it be. It was shortcircuitting the logic but inadequately covered. You are right. It's now adequately covered per PHV's expansion. Thanks.

- tsuji
 
Thank you PHV and Tsuji once again. That snipit of code looks promissing.
 
With a small amount of experimentation, the working code comes as thus:

Code:
surl ="[URL unfurl="true"]http://www.google.com/"[/URL]
set ie = nothing
set shapp=createobject("shell.application")

on error resume next

For Each owin In shapp.Windows

 	if left(owin.document.location.href,len(surl))=surl then

		if err.number = 0 then
		set ie = owin
  		end if
	    
	end if

err.clear

Next

on error goto 0

if ie is nothing then

	wscript.echo "Window Not Open"

else

	ie.navigate "[URL unfurl="true"]www.microsoft.com"[/URL]

end if
 
<A Further Note/>

Just to pursuit the shortciruit construction, this variation would work.
Code:
sURL="[URL unfurl="true"]http://www.google.com/"[/URL]
set oie=nothing
set shapp=createobject("shell.application")

on error resume next
for each owin in shapp.windows
   if not (strcomp(sURL,owin.document.location.href,1)=0) then
   else
       wscript.echo owin.document.location.href    'just checking
       set oie=owin
   end if
next
on error goto 0

set owin=nothing : set shapp=nothing

if not oie is nothing then
    'here you have got the searched object
    'and can manipulate it, for instance navigate.
    oie.navigate "[URL unfurl="true"]www.microsoft.com"[/URL]
end if
set oie=nothing
Thanks for your attention.

- tsuji
 
Hey, that would also work, I did notice in all my debugging that it wouldn't execute an 'else' if there was an error in the original logical test of the 'if'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top