R
Rich007
Hi,
I originally posted this under Word Programming, but I've not had any
response, so I thought I should try my luck with the friendly Outlook
Experts...
I've managed to pull together this code which tests whether Word is used as
the e-mail editor by Outlook (all Office 2003). Running the code from Word,
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...
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!
Why doesn't Outlook quit with this code?
Also, is there an easier way to do this by interogating the Outlook settings
in the registry, thus completely avoiding having to open Outlook?
Many thanks. Here's the code (all in a Word VBA module):
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
Set olApp = Nothing
Exit Function
ErrorHandler:
MsgBox "Error " & Err.Number & " " & Err.Description
End Function
Cheers
Rich
I originally posted this under Word Programming, but I've not had any
response, so I thought I should try my luck with the friendly Outlook
Experts...
I've managed to pull together this code which tests whether Word is used as
the e-mail editor by Outlook (all Office 2003). Running the code from Word,
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...
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!
Why doesn't Outlook quit with this code?
Also, is there an easier way to do this by interogating the Outlook settings
in the registry, thus completely avoiding having to open Outlook?
Many thanks. Here's the code (all in a Word VBA module):
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
Set olApp = Nothing
Exit Function
ErrorHandler:
MsgBox "Error " & Err.Number & " " & Err.Description
End Function
Cheers
Rich