Testing if Outlook uses Word, but can't quit Outlook

R

Rich007

Hi,
I've managed to pull together this code which tests whether Word is used as
the e-mail editor by Outlook (all Office 2003). It appears to work well,
except that if it had to open its own Outlook application (i.e. if Outlook
was NOT running at the start) it is supposed to Quit Outlook with the
olApp.Quit line. But at the end I still have a running Outlook.exe process
in my task manager! Even more strange is that if I run the code a second
time it can't use the existing Outlook application and enters the "If Err <>
0 Then" loop, so it's as if Outlook was closed, but stayed in the
TaskManager...

Why doesn't Outlook quit with this code and do I really need the three "Set
xxxx = Nothing" lines at the bottom?

Many thanks. Here's the code:

Sub CallTestOutlook()
Dim WordIsUsedByOutlook As String

If TestOutlook = True Then
WordIsUsedByOutlook = "IS"
Else
WordIsUsedByOutlook = "IS NOT"
End If

MsgBox "Word " & WordIsUsedByOutlook & " used by Outlook."
End Sub
Function TestOutlook() As Boolean
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Dim olInspector As Outlook.Inspector
Dim bStarted As Boolean

'Get a handle on the Outlook Application (if it is running)
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")

'If Outlook is not running then start Outlook
If Err <> 0 Then
On Error GoTo ErrorHandler
Set olApp = CreateObject("Outlook.Application")
bStarted = True
Else
On Error GoTo ErrorHandler
End If

'Create a MailItem and and Inspector for it
Set olMail = olApp.CreateItem(olMailItem)
Set olInspector = olMail.GetInspector

'Test whether Word is used by Outlook as e-mail editor
If olInspector.IsWordMail Then
TestOutlook = True
Else
TestOutlook = False
End If

Set olInspector = Nothing
Set olMail = Nothing

'Close Outlook if it was started by this macro.
If bStarted Then
olApp.Quit
End If

'Clean up
Set olApp = Nothing
Exit Function
ErrorHandler:
MsgBox "Error " & Err.Number & " " & Err.Description
End Function


Cheers
Rich
 
R

Rich007

Furthermore, if I then start Outlook from the start menu, a second
Outlook.exe appears in the TaskManager and the new Outlook hangs with a
white, empty window!
 

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