Nicolas
Nicolas

Reputation: 2376

How to call the quit/close event in outlook 2007 VBA

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

Answers (1)

Eugene Astafiev
Eugene Astafiev

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

Related Questions