Outlook 2003 Instance Won't Close If Opened Programmatically

G

Gman

Hi NG,

I recently modified some emailing code in one of my existing VB6
applications such that when I create an Outlook instance I make it
visible. I now find that when the app *creates* the instance (there's no
problem if I use GetObject to grab an existing instance) whether Outlook
is closed by my app or by the user (ALT F4, File Exit, X button etc)--
even after my application has closed -- Outlook just disappears but
still appears in Task Manager.

After half a day of troubleshooting I created a tiny app with one button
that runs the code pasted below. (That's the only code in the
application other than a button click event to run the sub.)

I'm running Outlook 2003 (11.6568.6568) SP2 on XP Home. Note that I
don't actually have an exchange server to connect to - I use this purely
for development.

I ran it on a Win2K machine running Office 2000 and everything behaved
perfectly. Note further that it also runs fine (i.e. I can close Outlook
w/o problems) on my XP machine providing I don't make Outlook visible.

There's posting galore on this topic - which I've read - I don't really
see what I can be doing incorrectly.

Any help gratefully received.

Thanks

Sub OpenOutlook()

Dim oOL As Object

'Try and get an active instance:
On Error Resume Next
Set oOL = GetObject(, "Outlook.Application")
On Error GoTo 0

If oOL Is Nothing Then
'let's try and create a new instance
Set oOL = CreateObject("Outlook.Application")
If Not oOL Is Nothing Then

Dim myNameSpace As Object, myFolder As Object, myExpl As Object
Set myNameSpace = oOL.GetNameSpace("MAPI")

'if we do find it, we should make it visible'
'there's (at least) two ways of doing this - both of
'which give me the same problem
If MsgBox("Use Explorer method? " _
& "(Otherwise, will use folder method...)" _
, vbYesNo) = vbYes Then

Set myExpl = _
oOL.explorers.Add(myNameSpace.GetDefaultFolder(6), 0)
'''olFolderInbox,'olFolderDisplayNormal
myExpl.Activate

Else
Set myFolder = myNameSpace.GetDefaultFolder(6)
'olFolderInbox
myFolder.Display

End If

Set myFolder = Nothing
Set myExpl = Nothing
Set myNameSpace = Nothing

End If
End If

'Here's some code to delay closing Outlook
'I put this here for testing purposes - I get the problem
'with or without this - i.e. whether I close OL
'programmatically or manually
If MsgBox("Outlook created = " & (oOL Is Nothing) _
& vbCrLf & "Wanna close?", vbYesNo) = vbYes Then _
oOL.application.quit

'tidy up....
Set oOL = Nothing

End Sub
 
M

Michael Bauer

Am Thu, 05 Jan 2006 22:32:28 -0600 schrieb Gman:

I think, closing the Explorer by code, if you created the OL instance,
should do it.
 
G

Gman

Hi Michael,

Thanks for your response. Unfortunately, if I use
myExpl.Close
then
oOL.Application.Quit
and then I release any objects associated with Outlook

Set myFolder = Nothing
Set myExpl = Nothing
Set myNameSpace = Nothing
Set oOL = Nothing

it still doesn't allow my application to kill Outlook.

Also, I'm not really looking to kill Outlook from my application
(although I would like to be able to do this). Rather, when I need to
send emails, I want to open up Outlook (if it's not already open) and
send them, leaving it open for the user.

If the user closes my application, I would prefer to leave Outlook
running - they may well be using it. But when they come to close Outlook
themselves, it won't close if I created the instance -- even if my
application is no longer open.

Thanks

Revised code:
Sub OpenOutlook2()

Dim oOL As Object
Dim myNameSpace As Object, myExpl As Object

'Try and get an active instance:
On Error Resume Next
Set oOL = GetObject(, "Outlook.Application")
On Error GoTo 0

If oOL Is Nothing Then
'try and create a new instance
Set oOL = CreateObject("Outlook.Application")
If Not oOL Is Nothing Then

Set myNameSpace = oOL.GetNamespace("MAPI")

Set myExpl = _
oOL.Explorers.Add(myNameSpace.GetDefaultFolder(6), 0)
'olFolderInbox,'olFolderDisplayNormal
myExpl.Activate
MsgBox "opening...."

End If
End If

If MsgBox("Outlook created = " & (oOL Is Nothing) _
& vbCrLf & "Wanna close?", vbYesNo) = vbYes Then
myExpl.Close
oOL.Application.Quit
End If

'tidy up....
Set myExpl = Nothing
Set myNameSpace = Nothing
Set oOL = Nothing

End Sub
 
M

Michael Bauer

Am Fri, 06 Jan 2006 08:59:26 -0600 schrieb Gman:

If the user can´t quit OL then your app is still running - or any other app
that holds at least one ref on OL. You need to check your code that really
nothing remains alive. A popular mistake e.g. is not to unload a form.
 
G

Gman

Hi Michael,

I understand your post - but I believe this isn't the case since:

(a) The app in question is a test application consisting of one form and
the code at the foot of this post -- that's it! You can see that
everything is released - there are no other variables or objects in the
application.

(b) If Outlook was opened by my app and then my app is closed -- I check
in Task Manager that my app is no longer running, when I try and
manually close Outlook I still get the problem. Therefore it's not my
app holding onto something, more it's Outlook not being opened / made
visible correctly. Maybe it's something to do with the namespace?!

(c) It works fine on Outlook 2000 under Win2K.

Thanks again,
Gman

'entire code of project
Option Explicit

Private Sub butClose_Click()
Unload Me
End Sub

Private Sub butGetOutlook_Click()
Call OpenOutlook
End Sub

Sub OpenOutlook()

Dim oOL As Object
Dim myNameSpace As Object, myExpl As Object

'Try and get an active instance:
On Error Resume Next
Set oOL = GetObject(, "Outlook.Application")
On Error GoTo 0

If oOL Is Nothing Then
'try and create a new instance
Set oOL = CreateObject("Outlook.Application")
If Not oOL Is Nothing Then

Set myNameSpace = oOL.GetNamespace("MAPI")

Set myExpl = _
oOL.Explorers.Add(myNameSpace.GetDefaultFolder(6), 0)
'olFolderInbox,'olFolderDisplayNormal
myExpl.Activate

End If
End If

'tidy up....
Set myExpl = Nothing
Set myNameSpace = Nothing
Set oOL = Nothing

End Sub
 
M

Michael Bauer

Am Mon, 09 Jan 2006 08:43:21 -0600 schrieb Gman:

Sorry, I don´t know what the problem is. If weird things happen then NAV is
often a good point to look at... :)
 
G

Gman

OK - I'll take a look at that. Possibly it's just my Outlook
installation. I've had no complaints from clients who use the full
application - but then again maybe they haven't noticed...

Thanks for getting back to me - I appreciate your time.

Gman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top