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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refer to existing instance

Status
Not open for further replies.

lorenzodv

Programmer
Feb 16, 2001
95
IT
Hello.

I used OLE automation to control a InternetExplorer object. This is the code I put in the Declarations section:

Private MyIE As SHDocVw.InternetExplorer

And this is how I created the object in Form_Load:

Set MyIE = CreateObject("InternetExplorer.Application")
MyIE.Visible = True

However, it always create a new instance of IE, while I want it to refer to an already running copy of explorer. I've tried with GetObject, but all I get is the "ActiveX component can't create the object" error.

Please help me.
Thanks in advance.

--
Lorenzo
 
Create the IE object once into a global object variable and re-use it. Be aware that the user can close the IE desoite your holding a reference to it so always use On error logic to intercept errors.
 
John, can you give me code for that. I put the object declarations in the declarations section of the form... do you mean I should have put it in a module?
 
Whatever will keep the object alive for as long as you want it. Defdine a module if the form does not live as long as the reference to IE must. Use a function to returm the IE object to a temprary object variable when you want IE.
Code:
'Example IE handler Module
Private mobjIE as object ' OR InternetExplorer.Application
Public Function GetIEObject() as object ' OR InternetExplorer.Application
    ' Veriy that we have an IE Automation object and
    ' that it is still present (user could have closed it).
    Dim blnW      as boolean 
    if mobjIE is nothing then ' Need IE
    else
        On error Resume next
            ' Dummy operation to determine if IE still there
            blnW = mobjIE.Visible ' Err if object BUT IE gone 
            if Err <> 0 then Set mobjIE = Nothing ' 
        On Error GOTO 0
    ' If there is no IE then create a new instance.
    if mobjIE IS Nothing then     
        set mobjIE CreateObject(&quot;InternetExplorer.Application&quot;)
    End if
    Set GetIEObject = mobjIE  ' Return the IE Object
End sub
Public Sub CloseIE() ' Jist on general principle
    Set mobjIE = Nothing
End sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top