reference an open instance of IE

  • Thread starter Thread starter Atishoo
  • Start date Start date
A

Atishoo

Does anyone know how to reference an already open instance of IE in VBA?
rather than
Set ie = CreateObject("InternetExplorer.Application")
which creates a new instance.
 
Try the function below.

Tim


'***********************************************
'Usage...
dim ie as object
set ie = GetIE("http://www.google")
if not ie is nothing then
msgbox "found IE!"
'do stuff with ie
else
msgbox "IE not found!"
end if


'Find an IE window with a matching (partial) URL
'Assumes no frames.
Function GetIE(sAddress As String) As Object

Dim objShell As Object, objShellWindows As Object, o As Object
Dim retVal As Object, sURL As String


Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows

'see if IE is already open
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.document.Location
On Error GoTo 0
If sURL <> "" Then
If sURL Like sAddress & "*" Then
Set retVal = o
Exit For
End If
End If
Next o

Set GetIE = retVal
End Function
'*************************************
Tim
 
Back
Top