Reputation: 2376
The question is simple, yet hard to achieve for me for some reason. How can I get to fire the quit/close event when outlook 2007 is being closed?
I want do display a Yes/No msgbox in VBA which executes code depending on the option chosen when outlook is being closed.
I thought I had the solution using:
Dim WithEvents myOlApp As Outlook.Application
Sub Initialize_handler()
Set myOlApp = CreateObject("Outlook.application")
End Sub
Private Sub myOlApp_Quit()
MsgBox "TEST"
End Sub
First I tried to insert it into my Module but this gave me the Only valid in object modules
error. So then I created a new class module and pasted the code in here (which gave no errors) but still the event wont fire. What is going wrong and how to fix it?
Upvotes: 2
Views: 812
Reputation: 49397
Set myOlApp = CreateObject("Outlook.application")
There is no need to create a new Outlook instance. You should use the Application property availble in Outlook VBA.
Private Sub Application_Quit()
MsgBox "Goodbye, " & Application.GetNamespace("MAPI").CurrentUser
End Sub
Take a look at the Getting Started with VBA in Outlook 2010 article in MSDN.
Upvotes: 1