Link to home
Start Free TrialLog in
Avatar of Dalexan
DalexanFlag for Afghanistan

asked on

Detect if object has been closed

I have an IE object that the user enters information in and has the choice to close it once complete. Some users are not closing the window, I want to check if the object is open and quit. Here's the code thats not working. I get an error # 462 the remote server machine does not exist or is unavailable.

            If objIE.Visible = True Then
                objIE.Quit
                Set objIE = Nothing
            End If

Here's the code for objIE, its in its own module:

Option Explicit
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Integer) As Integer
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
Const MF_BYPOSITION = &H400
Public objIE As Object

Public Sub LaunchIE(ByVal strUrl As String, ByVal blnToolBar As Boolean, ByVal blnStatusBar As Boolean)
    Dim SysMenu As Long, Res As Long
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Toolbar = blnToolBar
    objIE.StatusBar = blnStatusBar
    objIE.Visible = True
    Call objIE.Navigate(strUrl)
   
    SysMenu = GetSystemMenu(objIE.hwnd, 0)
    Res = RemoveMenu(SysMenu, 6, MF_BYPOSITION)
   
    'Set objIE = Nothing
End Sub
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Click on Project --> References and check the "Microsoft Internet Controls" entry.

This must be in a form since we are using WithEvents...but this allows you to handle the events of your IE instance.

Note in Command1_Click() how we can check to see if the "IE" variable points to Nothing.  This is possible because we set the variable back to Nothing in the instances OnQuit() event.

' --------------------
'  Form 1
' --------------------
Option Explicit

Private WithEvents IE As InternetExplorer

Private Sub Command1_Click()
    If IE Is Nothing Then
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True
    End If
    IE.Navigate "some web address"
End Sub

Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    ' do something in here...
End Sub

Private Sub IE_OnQuit()
    Set IE = Nothing
End Sub
Avatar of Dalexan

ASKER

I cannot add the control, is there another way to do this?
There are no controls to add...just a Reference.

Do you mean you can't add a Form to your Project?
Avatar of Dalexan

ASKER

If I add that reference then i will need to update the msinet.dll to all my 750 user machines.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It uses:

    C:\WINDOWS\SYSTEM32\shdocvw.dll
Avatar of Dalexan

ASKER

I guess i will try entering this into a class
Avatar of Dalexan

ASKER

Ok how do I check if the object has been closed and whats the below used for?

Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    ' do something in here...
End Sub
The DocumentComplete() event fires when the page finishes loading.  It is not necessary for your problem and is simply there to demonstrate some of the other events you can trap.

The OnQuit() event fires when the IE instance is closed.  You can put code in there to do something when the browser is closed.

Here we check to see if the "IE" variable is Nothing:

    If IE Is Nothing Then

If this returns true, then either no instance has been created yet, or the previous instance was already closed.  You could put a boolean flag into you class to track whether an instance was created or not.
Avatar of Dalexan

ASKER

I get a user defined type not defined message on Public WithEvents objIE As InternetExplorer
Did you add the reference to your project?

Click on Project --> References and check the "Microsoft Internet Controls" entry.
Avatar of Dalexan

ASKER

I call this object many times to get the source of the html and parse it out to enter into fields in my order entry app. I get a variable not defined when calling this in my form. I declared objIE as public in the class module though?

str = objIE.Document.documentElement.outerhtml
If "objeIE" is in a class then you need to reference that class:

    Dim c1 As New Class1 ' (more likely that you'll use a global instance of the class)
    c1.objeIE.something...
Avatar of Dalexan

ASKER

so i need to declare the class that the launchIE function is in within the form

Dim c1 as new ClassLaunchIE

then reference it as

str = c1.objIE.Document.documentElement.outerhtml

Here's what i have so far in the class module named ClassLaunchIE:

Option Explicit
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Integer) As Integer
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
Const MF_BYPOSITION = &H400
Public WithEvents objIE As InternetExplorer

Public Sub LaunchIE(ByVal strURL As String, ByVal blnToolBar As Boolean, ByVal blnStatusBar As Boolean)
    Dim SysMenu As Long, Res As Long
    If objIE Is Nothing Then
        Set objIE = CreateObject("InternetExplorer.Application")
        objIE.Visible = True
    End If
    objIE.Toolbar = blnToolBar
    objIE.StatusBar = blnStatusBar
    objIE.Visible = True
    objIE.Navigate strURL
   
    SysMenu = GetSystemMenu(objIE.hwnd, 0)
    Res = RemoveMenu(SysMenu, 6, MF_BYPOSITION)
   
End Sub

Private Sub objIE_OnQuit()
    Set objIE = Nothing
End Sub
That's making more sense...  =)

Are you still getting an error?
Avatar of Dalexan

ASKER

That compiled but im getting an error inside my app when running. I will have to finish this tommorrow as they are locking the doors on me if I dont get out of here soon.
Good night.... =)
Avatar of Dalexan

ASKER

I am getting a program error box that says.

"orderpoint.exe has generated errors and will be closed by windows. you will need to restart the program."

Orderpoint.exe is my application where im closing the ie window.

Can I suppress this box so the user doesnt see it?
That's not an error you can trap and suppress.

Something in the code needs to be fixed...need to troubleshoot more.

It's such a generic error msg...wouldn't know where to start without seeing basically all the code. =\
Avatar of Dalexan

ASKER

I have read in other posts it may be caused by the way I'm closing the IE window out. That code is posted below.

        If Not c1.objIE Is Nothing Then    'if the IE window is open quit it
            c1.objIE.Quit
            Set c1.objIE = Nothing
        End If