Solved

Detect if object has been closed

Posted on 2006-06-14
20
334 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
  • 10
  • 10
20 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
I cannot add the control, is there another way to do this?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
There are no controls to add...just a Reference.

Do you mean you can't add a Form to your Project?
0
 

Author Comment

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

Accepted Solution

by:
Mike Tomlinson earned 150 total points
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
It uses:

    C:\WINDOWS\SYSTEM32\shdocvw.dll
0
 

Author Comment

by:Dalexan
Comment Utility
I guess i will try entering this into a class
0
 

Author Comment

by:Dalexan
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
I get a user defined type not defined message on Public WithEvents objIE As InternetExplorer
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Did you add the reference to your project?

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

Author Comment

by:Dalexan
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
That's making more sense...  =)

Are you still getting an error?
0
 

Author Comment

by:Dalexan
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Good night.... =)
0
 

Author Comment

by:Dalexan
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now