?
Solved

Detect if object has been closed

Posted on 2006-06-14
20
Medium Priority
?
500 Views
Last Modified: 2010-04-07
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
0
Comment
Question by:Dalexan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
20 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16905827
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
0
 

Author Comment

by:Dalexan
ID: 16905888
I cannot add the control, is there another way to do this?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16905902
There are no controls to add...just a Reference.

Do you mean you can't add a Form to your Project?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Dalexan
ID: 16905945
If I add that reference then i will need to update the msinet.dll to all my 750 user machines.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 600 total points
ID: 16905946
You can do essentialy the same thing but with a Class:

' --------------------
'  Class1
' --------------------
Private WithEvents IE As InternetExplorer

Public Sub OpenAddress(ByVal URL As String)
    If IE Is Nothing Then
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True
    End If
    IE.Navigate URL
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16905970
It uses:

    C:\WINDOWS\SYSTEM32\shdocvw.dll
0
 

Author Comment

by:Dalexan
ID: 16905996
I guess i will try entering this into a class
0
 

Author Comment

by:Dalexan
ID: 16906236
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16906452
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.
0
 

Author Comment

by:Dalexan
ID: 16906802
I get a user defined type not defined message on Public WithEvents objIE As InternetExplorer
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16906823
Did you add the reference to your project?

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

Author Comment

by:Dalexan
ID: 16906879
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16906910
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...
0
 

Author Comment

by:Dalexan
ID: 16906958
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16906978
That's making more sense...  =)

Are you still getting an error?
0
 

Author Comment

by:Dalexan
ID: 16907074
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.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16907110
Good night.... =)
0
 

Author Comment

by:Dalexan
ID: 16912414
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?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16912454
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. =\
0
 

Author Comment

by:Dalexan
ID: 16912506
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question