PDA

View Full Version : Popup when we close outlook



Ashish10
05-24-2013, 12:39 PM
I am trying to develop a simple macro wherein I should get a pop up window everytime I close the outlook. I have put the codes which closes the outlook when I run it using run macro but when I hit close using mouse it does not give a popup.
Is there a way to get it. It is similar like, when we try to send an email without subject line, it gives a pop up. The same way I am trying for when I hit the close button. So far I have done this. I am an intermediate level VBA user


Sub CloseOutlook()
Dim objOLook As Object
Set objOLook = GetObject(, "Outlook.Application")
objOLook.Application.Quit
i = MsgBox("Do you want to set OOO ?", vbYesNo, "Outlook", "test.hlp", 100)
End Sub

skatonni
05-28-2013, 12:56 PM
See here for an example http://office.microsoft.com/en-ca/outlook-help/HV080800658.aspx

Place the code in the ThisOutlookSession module

Private Sub Application_Quit()
Dim i As VbMsgBoxResult
i = MsgBox("Do you want to set OOO ?", vbYesNo, "Outlook", "test.hlp", 100)
End Sub

Ashish10
05-28-2013, 09:53 PM
Great! That works for my first part.
Thanks skatonni

Now, what I am trying is, when I / user clicks on Yes, Outloook should not close. When clicked on No, it should close.

What I have done below doesn't work. In both the cases, it quits. Where am I going wrong.

Private Sub Application_Quit()

Dim objAppOL As Outlook.Application
Set objAppOL = GetObject(Class:="Outlook.application")

Dim i As VbMsgBoxResult

If i = MsgBox("Do you want to set OOO ?", vbQuestion + vbYesNo) = vbNo Then
objAppOL.Quit

Else
Set objAppOL = Nothing

End If

End Sub

skatonni
05-30-2013, 04:43 PM
Great! That works for my first part.
Thanks skatonni

Now, what I am trying is, when I / user clicks on Yes, Outloook should not close. When clicked on No, it should close.

What I have done below doesn't work. In both the cases, it quits. Where am I going wrong.

Private Sub Application_Quit()

Dim objAppOL As Outlook.Application
Set objAppOL = GetObject(Class:="Outlook.application")

Dim i As VbMsgBoxResult

If i = MsgBox("Do you want to set OOO ?", vbQuestion + vbYesNo) = vbNo Then
objAppOL.Quit

Else
Set objAppOL = Nothing

End If

End Sub

I do not recognize
If i = MsgBox("Do you want to set OOO ?", vbQuestion + vbYesNo) = vbNo Then

It does not throw an error, but the following might work if there was a Cancel parameter in Application_Quit.

Private Sub Application_Quit(Cancel As Boolean)
If MsgBox("Do you want to set OOO ?", vbQuestion + vbYesNo) = vbYes Then
Cancel = True
End If
End Sub

Since there is no Cancel you have to programatically set the Out of Office within Application_Quit. See here 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

Outlook 2010 displays a yellow bar warning you to turn Out of Office off but not if you use the above code you may want to use the Application_Startup code provided.


For Outlook 2003 and prior

Sub OutOfOffice(bolState As Boolean)
Dim mapSession As Object
Set mapSession = CreateObject("MAPI.Session")
With mapSession
.Logon , , False, False, 0
.OutOfOffice = bolState
.Logoff
End With
Set mapSession = Nothing
End Sub

Ashish10
05-30-2013, 11:51 PM
Thanks alot. I will try the codes this weekend.
you are really helpful.