Tuesday, November 10, 2009

Conencting to open internet explorer instance

We come across this situation many times wherein we need to connect to some application already invoked by some other part of code. Simple goggling gives examples about invoking an application but there are very few examples which explains connecting to already open application. Here is small snippet which enables user to connects to existing IE.
Dim objInstances, objIE, strURL
strURL = ""
Set objInstances = CreateObject("Shell.Application").windows
If objInstances.Count > 0 Then ' To make sure we have IE instance open.
For Each objIE In objInstances
Dim sName
sName = UCASE(objIE.FullName)
If Right(sName, 12) = "IEXPLORE.EXE" Then ' Searching for internet explorer
objIE.Navigate strURL ' Navigate to intended URL
Exit For
End if
wscript.echo "No explorer windows are open."
End if

Sometimes there are simple solutions to complex problems, isn't it ?