Barok
Barok

Reputation: 151

Trigger ItemSend for certain outlook macros only

How would I modify the following code to trigger the event myMailItem_ItemSend only when the email is sent by myMacro1, but not in other cases (such as myMacro2)?

The event should be triggered especially for those macros using the myMailItem object.

Public WithEvents myMailItem As Outlook.MailItem

Public Sub Initialize_handler() 
    Set myMailItem = Outlook.MailItem
End Sub 

Private Sub myMailItem_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    Dim prompt As String 
    prompt = "Are you sure you want to send " & Item.Subject & "?" 
    If MsgBox(prompt, vbYesNo + vbQuestion, "Send confirmation") = vbNo Then 
        Cancel = True 
    End If 
End Sub

'Should trigger the send confirmation msgbox
Private Sub myMacro1()
    Dim objApp As Outlook.Application
    Set objApp = Application
    Set myMailItem = objApp.ActiveInspector.CurrentItem.ReplyAll
    myMailItem.Display
End Sub

'Should NOT trigger the send confirmation msgbox
Private Sub myMacro2()
    Dim objApp As Outlook.Application
    Set objApp = Application
    Dim oEmail As Outlook.mailItem
    Set oEmail = objApp.ActiveInspector.CurrentItem.ReplyAll
    oEmail.Display
End Sub

Your kind help would be appreciated.

Upvotes: 0

Views: 248

Answers (1)

Matteo NNZ
Matteo NNZ

Reputation: 12655

I would go for this:

  1. Define a global variable in your module, such as Dim TriggerMsgBox As Boolean. By default, the variable will be false.
  2. Initialize it as True in the myMacro1(). Only in that case, it will become True. Else, it will be False.
  3. Use it in the myMailItem_ItemSend event: if the variable is True (meaning we just passed by myMacro1()), then you need to prompt the MsgBox. Else, you will just pass by. Of course, don't forget to reset the variable to False after the MsgBox is hit, else you will keep on showing it even later.

In your code it would be:

Public WithEvents myMailItem As Outlook.MailItem
Dim TriggerMsgBox As Boolean '<-- NEW LINE OF CODE

Public Sub Initialize_handler() 
    Set myMailItem = Outlook.MailItem
End Sub 

Private Sub myMailItem_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    Dim prompt As String 
    If TriggerMsgBox Then '<-- NEW LINE OF CODE
        TriggerMsgBox = False '<-- NEW LINE OF CODE
        prompt = "Are you sure you want to send " & Item.Subject & "?" 
        If MsgBox(prompt, vbYesNo + vbQuestion, "Send confirmation") = vbNo Then 
            Cancel = True 
        End If
    End If '<-- NEW LINE OF CODE
End Sub

'Should trigger the send confirmation msgbox
Private Sub myMacro1()
    Dim objApp As Outlook.Application
    Set objApp = Application
    Set myMailItem = objApp.ActiveInspector.CurrentItem.ReplyAll
    TriggerMsgBox = True '<-- NEW LINE OF CODE
    myMailItem.Display
End Sub

'Should NOT trigger the send confirmation msgbox
Private Sub myMacro2()
    Dim objApp As Outlook.Application
    Set objApp = Application
    Dim oEmail As Outlook.mailItem
    Set oEmail = objApp.ActiveInspector.CurrentItem.ReplyAll
    oEmail.Display
End Sub

Upvotes: 2

Related Questions