PDA

View Full Version : Quit event code - Outlook 2007



TheAntiGates
08-13-2013, 08:40 AM
I want to intercept Outlook shutdown if there are windows open (emails, reminders) and challenge myself with a yes/no whether to indeed shutdown, like it does when you have "semi-deleted" items, or items that have changed since opening (and emails that have an attachment open, so it prompts you before closing the email). Maybe I'll have a yes/no/cancel msgbox for each active window found, stating its .Name. Anyway to to get cranking I started off in ThisOutlookSession with

Dim olApp As Outlook.Application
Private Sub Application_Quit()
Dim olItems As Outlook.Items, olItem As Object, i As Integer

For Each olItem In olItems 'breakpoint on this line
Next
End Subwith the aim of perhaps going
for each Explorer in olApp
or
for each Window in olApp
I'm just spitballing at this point; I'm weak on these objects.

However using the compiled code as shown the breakpoint in the Quit event only flashes for a second, and then Outlook closes anyway.
A. I request to close OL07 with File/Exit
B. It asks whether to permanently delete Deleted Items
C. I click yes
D. The breakpoint line lights up as though I'll get debug control over execution.
E. A half a second later Outlook disappears and OUTLLOOK.EXE disappears from XP task list.

So my questions are:
1. Is Application_Quit() the place to do this?
2. Can I EVEN stop the Quit anyway? I don't see a "Cancel" boolean, for one thing!
3. Is there a "collection" of open windows that I can For Each upon?

Thanks.

skatonni
08-14-2013, 01:55 PM
Maybe you can make this work, I cannot.

It is the bare bones without the code to initiliaze objExplorer and objInspector.

BEFORE Application_Quit event

http://www.office-archive.com/36-outlook/5cc5ec1793dc7631.htm

You have to trap both the Inspector and Explorer Close events. In
those events you check how many Inspectors and Explorers are currently
open.


Private Sub objExplorer_Close()
On Error Resume Next
If objOutlook.Explorers.Count <= 1 Then
'do whatever
End If
End Sub

Private Sub objInspector_Close()
On Error Resume Next

If objOutlook.Explorers.Count = 0 And objOutlook.Inspectors.Count <= 1 Then
'do whatever
End If
End Sub

TheAntiGates
08-14-2013, 04:12 PM
Thanks! Here is how to initialize, in ThisOutlookSession:
Public WithEvents myOlExplorers As Outlook.Explorers
Public WithEvents objReminders As Outlook.Reminders
Private WithEvents olInsp As Inspectors

Public Sub Initialize_Handler()
Set myOlExplorers = Application.Explorers
Set objReminders = Application.Reminders
Set olInsp = Application.Inspectors
End SubWith initialization maybe your code will function as is; again, though, I don't see a "Cancel" Boolean there either so it looks like I'm sunk. Besides, I wonder what the heck they even have a Quit event for anyway - you can't even use a breakpoint there. WTG, MS developer geniuses.

skatonni
08-19-2013, 09:58 AM
... I wonder what the heck they even have a Quit event for anyway - you can't even use a breakpoint there. WTG, MS developer geniuses.

No other ideas on your original question but although the quit event is frustrating, it can be useful.

For example, you can set your Out of Office message. http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/A_3487-Automating-Out-of-Office-in-Outlook.html


Private Sub Application_Quit()
OutOfOffice True
End Sub

Private Sub Application_Startup()
'Remove this subroutine if you do not want to turn OOF off automatically.'
OutOfOffice False
End Sub

Sub OutOfOffice(bolState As Boolean)
Const PR_OOF_STATE = "http://schemas.microsoft.com/mapi/proptag/0x661D000B"
Dim olkIS As Outlook.Store, olkPA As Outlook.PropertyAccessor
For Each olkIS In Session.Stores
If olkIS.ExchangeStoreType = olPrimaryExchangeMailbox Then
Set olkPA = olkIS.PropertyAccessor
olkPA.SetProperty PR_OOF_STATE, bolState
End If
Next
Set olkIS = Nothing
Set olkPA = Nothing
End Sub