PDA

View Full Version : [SOLVED:] To run a VBA when closing the email or after pressing escape button.



abdulmalik01
05-25-2022, 03:27 AM
Hi All,

I would like to run a macro in outlook. This macro should get triggered in when i close a email or when press escape to close the email.

Is there any sample i can refer to.

Regards

abdulmalik01
05-25-2022, 04:21 AM
Hi All,

I would like to run a macro in outlook. This macro should get triggered in when i close a email or when press escape to close the email.

Is there any sample i can refer to.

Regards

i have tried the below but its not getting executed it seems but nothing is happening



Public WithEvents colInspectors As Outlook.Inspectors
Public WithEvents myInspector As Outlook.Inspector

Private Sub Application_Startup()
MsgBox "Test1"
Set colInspectors = Application.Inspectors
End Sub

Private Sub col_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then ' only for mail items
MsgBox "Test2"
Set myInspector = Inspector
End If
End Sub

Private Sub myInspector_Close()
MsgBox "Test3"
Set myInspector = Nothing
End Sub

georgiboy
05-27-2022, 07:31 AM
Try putting this into the ThisOutlookSession code module, close outlook and then reopen.
Open a new mail and then try to close it. You can see the code is running when you try to close the mail or esc the mail:

Dim WithEvents Insps As Inspectors
Dim WithEvents mItem As MailItem


Private Sub Application_Startup()
Set Insps = Application.Inspectors
End Sub


Private Sub Insps_NewInspector(ByVal Inspector As Inspector)
If TypeOf Inspector.CurrentItem Is MailItem Then
Set mItem = Inspector.CurrentItem
End If
End Sub


Private Sub mItem_Close(Cancel As Boolean)
MsgBox "Unable to close due to georgiboy!"
Cancel = True
End Sub

abdulmalik01
05-27-2022, 08:01 AM
Georgiboy... its working.. thank you very much...