Outlook wont .quit

Status
Not open for further replies.
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
 
K

Ken Slovak - [MVP - Outlook]

I fail to see why Outlook wouldn't quit, see if adding a line like this

would help:

olApp.Session.Logon "", "", False, False

'Create a MailItem and and Inspector for it

Set olMail = olApp.CreateItem(olMailItem)

You can check at

HKCU\Software\Microsoft\Office\11.0\Outlook\Options\Mail\UseWordMail (a

REG_DWORD, 1 = yes). The "11.0" is specific to Outlook 2003.

"Rich007" <Rich007> wrote in message

news:D9CF2B5E-BE00-4BC7-B533-B68682B51052@microsoft.com...
> 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
>
 
R

Rich007

Hi Ken, thank you so much for responding.

I tried your suggested extra line of code before creating the MailItem, but

it made no difference. So I tried gradually commenting out lines of code

until Outlook does quit. It appears to be the line:

Set olInspector = olMail.GetInspector

that is causing the issue. If I don't call that line Outlook quits as

ordered. If I do include that line, Outlook stays in the TaskManager. Any

ideas?

Thanks also for providing the registry address. I have never used VBA to

interogate the registry. Would you recommend I use the API route as

described here: http://support.microsoft.com/default.aspx/kb/145679 ...or

go down the scripting route as described here:

http://vba-corner.livejournal.com/3054.html ?

(these are the first two results I've found in Google).

Thanks again.

Cheers

Rich
wrote:


> I fail to see why Outlook wouldn't quit, see if adding a line like this
> would help:

> olApp.Session.Logon "", "", False, False

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

> You can check at
> HKCU\Software\Microsoft\Office\11.0\Outlook\Options\Mail\UseWordMail (a
> REG_DWORD, 1 = yes). The "11.0" is specific to Outlook 2003.

> >

>

> "Rich007" <Rich007> wrote in message
> news:D9CF2B5E-BE00-4BC7-B533-B68682B51052@microsoft.com...
> > 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
> >


>
 
K

Ken Slovak - [MVP - Outlook]

In that case try closing the Inspector object when you're done with it,

before you set it to Nothing.

Six of one, half dozen of another as far as reading the registry for that

value. What are you most comfortable with? The only caution is if someone is

running A-V with one of those script stoppers it might prevent the script

access from working.

"Rich007" <Rich007> wrote in message

news:FB50970E-FAD7-40E0-9BEB-8490815839AA@microsoft.com...
> Hi Ken, thank you so much for responding.
> I tried your suggested extra line of code before creating the MailItem,
> but
> it made no difference. So I tried gradually commenting out lines of code
> until Outlook does quit. It appears to be the line:
> Set olInspector = olMail.GetInspector
> that is causing the issue. If I don't call that line Outlook quits as
> ordered. If I do include that line, Outlook stays in the TaskManager.
> Any
> ideas?

> Thanks also for providing the registry address. I have never used VBA to
> interogate the registry. Would you recommend I use the API route as
> described here: http://support.microsoft.com/default.aspx/kb/145679
> ...or
> go down the scripting route as described here:
> http://vba-corner.livejournal.com/3054.html ?
> (these are the first two results I've found in Google).

> Thanks again.
> Cheers
> Rich
 
R

Rich007

That's it! Thank you Ken.

I needed to close BOTH the Inspector AND the MailItem before calling the

> .quit command. All works fine now, although I suspect I will interogate the

registry rather than Outlook itself, since Outlook is slow when it starts

(and if it is open for long enough the calendar reminders can pop up!). I

got the registry query functions working using the API functions provided at

this site:

http://support.microsoft.com/default.aspx/kb/145679

Incidentally, the 2 registry values that sets whether Word is used by

Outlook 2003 are at:

[HKCU\] Software\Microsoft\Office\11.0\Outlook\Options\Mail

"UseWordMail" is 1 if the "Use Microsoft Office Word 2003 to read Rich Text

e-mail messages" is checked (zero if not).

and

"EditorPreference" which corresponds to the "Use Microsoft Office Word 2003

to edit e-mail messages" checkbox has the following values:

Word is NOT used if EditorPreference is set to either:

' 131072 = HTML/Outlook

' 196610 = Rich Text/Outlook

' 65536 = Plain Text/Outlook

Word IS used if EditorPreference is set to either:

' 196609 = Rich Text/Microsoft Word

' 65537 = Plain Text/Microsoft Word

' 131073 = HTML/Microsoft Word

So my macro for interogating the registry has to test for all these values.

In case anyone else does want to work directly with Outlook to determine

these settings, here is the working function to do it (returns true or false).

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 an 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 'Function output

Else

TestOutlook = False 'Function output

End If

'Close the Inspector and the MailItem

olInspector.Close (olDiscard) 'MUST HAVE THESE FOR OUTLOOK TO QUIT!

olMail.Close (olDiscard)

'Close Outlook if it was started by this macro.

If bStarted Then

olApp.Quit

End If

'Clean up

Set olInspector = Nothing

Set olMail = Nothing

Set olApp = Nothing

Exit Function

ErrorHandler:

MsgBox "Error " & Err.Number & " " & Err.Description

End Function

Thanks again Ken.

Cheers

Rich
wrote:


> In that case try closing the Inspector object when you're done with it,
> before you set it to Nothing.

> Six of one, half dozen of another as far as reading the registry for that
> value. What are you most comfortable with? The only caution is if someone is
> running A-V with one of those script stoppers it might prevent the script
> access from working.

> >

>

> "Rich007" <Rich007> wrote in message
> news:FB50970E-FAD7-40E0-9BEB-8490815839AA@microsoft.com...
> > Hi Ken, thank you so much for responding.
> > I tried your suggested extra line of code before creating the MailItem,
> > but
> > it made no difference. So I tried gradually commenting out lines of code
> > until Outlook does quit. It appears to be the line:
> > Set olInspector = olMail.GetInspector
> > that is causing the issue. If I don't call that line Outlook quits as
> > ordered. If I do include that line, Outlook stays in the TaskManager.
> > Any
> > ideas?
> > Thanks also for providing the registry address. I have never used VBA to
> > interogate the registry. Would you recommend I use the API route as
> > described here: http://support.microsoft.com/default.aspx/kb/145679
> > ...or
> > go down the scripting route as described here:
> > http://vba-corner.livejournal.com/3054.html ?
> > (these are the first two results I've found in Google).
> > Thanks again.
> > Cheers
> > Rich


>
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J GoDaddy migrated to Office365 - Outlook Wont Add Account Exchange Server Administration 21
M Emails Wont Upload to from MS Outlook 2010 to Server Using Outlook.com accounts in Outlook 9
M Outlook wont send emails but receives Using Outlook 1
D Outlook 2013 wont send to over 3 addresses Using Outlook 1
B Outlook contact, calendars, and tasks wont set up in icloud Using Outlook 4
K outlook calendar wont sync iphone 4 Using Outlook 1
Fozzie Bear Outlook 2010 wont remember Exchange Credentials on log off /shutdown of Win7 Exchange Server Administration 2
P Calender in Outlook 2007 wont open Using Outlook 4
M Outlook wont start Using Outlook 1
R Outlook Connector wont sync with iPhone/iPad Using Outlook.com accounts in Outlook 1
Rupert Dragwater Background colors not saving in Outlook 365 Using Outlook 11
petunia Outlook tasks module sunsetting? Exchange Server Administration 3
G Save emails as msg file from Outlook Web AddIn (Office JS) Outlook VBA and Custom Forms 1
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 3
U Outlook 2021 not showing contact cards in Searches Using Outlook 2
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 2
H Outlook 365 O365 outlook calendar item editing Using Outlook 1
J Outlook 365 html inline images Using Outlook 1
Rupert Dragwater How to get Outlook 365 to open from websites Using Outlook 5
S Why do I have to close and reopen Outlook for macros to work? Outlook VBA and Custom Forms 2
J Outlook 2021 ScanPST errors (yet again ... sorry): repair button missing Outlook 2021 Using Outlook 0
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
K Moved pst to new computer, now Gmail not coming into Outlook Using Outlook 7
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
F Running Scripts in Outlook 2021 Using Outlook 0
Nufc1980 Outlook "Please treat this as private label" auto added to some emails - Help. Using Outlook 3
S Outlook 2019 Custom outlook Add-in using Visual Studio Outlook VBA and Custom Forms 0
V Outlook macros no longer run until VB editor is opened Outlook VBA and Custom Forms 0
R Outlook 365 How to integrate a third-party app with Outlook to track email and sms? Using Outlook 2
e_a_g_l_e_p_i I can't believe what I am seeing when trying to install Outlook 2021 Using Outlook 9
Kika Melo Outlook Calendar deleted appointments not in Deleted Items folder Using Outlook 3
P How to get a QR code for automatic signin with Outlook for iOS Using Outlook 5
J Migrating Outlook Using Outlook 1
Retired Geek Outlook for the MAC with Yahoo accounts now very broken Using Outlook 9
S Outlook 2002- "Send" button has disappeared. Help please. Using Outlook 1
L How Stop Outlook Nag Messages Using Outlook 1
TomHuckstep Remove Send/Receive All Folders (IMAP/POP) button from Outlook 365 Ribbon Using Outlook 1
L I Cannot Sign Into My Outlook Account? Outlook VBA and Custom Forms 0
icacream Outlook 2021 - Google calendar in the peek Using Outlook 0
e_a_g_l_e_p_i Question about installing my Gmail account on my iPhone but still getting messages downloaded to my desktop Outlook. Using Outlook 3
F Want to add second email to Outlook for business use Using Outlook 4
kburrows Outlook Email Body Text Disappears/Overlaps, Folders Switch Around when You Hover, Excel Opens Randomly and Runs in the Background - Profile Corrupt? Using Outlook 0
M using excel to sort outlook appointment items Outlook VBA and Custom Forms 4
e_a_g_l_e_p_i MY Outlook 2021 changed the format of the shortcuts for mail, calendar etc. Using Outlook 10
Z Outlook 2021 Outlook new emails notification not working Using Outlook 4
K Changing the Deleted Items location in Outlook 2019 Using Outlook 2
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
V How to use Comas in a picklist in Outlook forms Outlook VBA and Custom Forms 3
e_a_g_l_e_p_i Question about reinstalling Outlook 2021 Using Outlook 5
A Outlook 365 Outlook (part of 365) now working offline - argh Using Outlook 5

Similar threads

Top