Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

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
0
Dalexan
Asked:
Dalexan
  • 10
  • 10
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
DalexanAuthor Commented:
I cannot add the control, is there another way to do this?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
There are no controls to add...just a Reference.

Do you mean you can't add a Form to your Project?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
DalexanAuthor Commented:
If I add that reference then i will need to update the msinet.dll to all my 750 user machines.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It uses:

    C:\WINDOWS\SYSTEM32\shdocvw.dll
0
 
DalexanAuthor Commented:
I guess i will try entering this into a class
0
 
DalexanAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
DalexanAuthor Commented:
I get a user defined type not defined message on Public WithEvents objIE As InternetExplorer
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you add the reference to your project?

Click on Project --> References and check the "Microsoft Internet Controls" entry.
0
 
DalexanAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
DalexanAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That's making more sense...  =)

Are you still getting an error?
0
 
DalexanAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Good night.... =)
0
 
DalexanAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
DalexanAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now