Consulting

Results 1 to 4 of 4

Thread: To run a VBA when closing the email or after pressing escape button.

  1. #1

    To run a VBA when closing the email or after pressing escape button.

    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

  2. #2
    Quote Originally Posted by abdulmalik01 View Post
    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
    Last edited by Aussiebear; 05-25-2022 at 05:26 AM. Reason: Added code tags to supplied code

  3. #3
    VBAX Expert
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    774
    Location
    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
    If things don't change they stay the same
    Quite often there is a picnic problem (problem in chair not in computer)
    "We were not told it was impossible, so we did it."

  4. #4
    Georgiboy... its working.. thank you very much...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •