Search code examples
vbaoutlook

How to popup question, whether to flag, after pressing send button?


Is there a way to ask (popup) if I want to flag the email after I press the send button?


Solution

  • Use the Application.ItemSend event to display a MsgBox asking whether to flag or not.

    Then as noted in this question, you'll need to listen to the Items.ItemAdd event on the Sent Items folder and call MarkAsTask on the message passed to the event handler.

    So add the following code to ThisOutlookSession - use Alt + F11 to bring up the VB editor.

    enter image description here

    Private WithEvents Items As Outlook.Items
    
    Private Sub Application_Startup()
        Dim SentItems As Folder
        Set SentItems = Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail)
        Set Items = SentItems.Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal Item As Object)
        If TypeOf Item Is Outlook.MailItem Then
            Dim property As UserProperty
            Set property = Item.UserProperties("FlagForFollowUp")
    
            If property Is Nothing Then Exit Sub
    
            Item.MarkAsTask olMarkThisWeek
            Item.Save
        End If
    End Sub
    
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        If TypeOf Item Is Outlook.MailItem Then
            Dim prompt As String
            prompt = "Would you like to flag this item?"
    
            If MsgBox(prompt, vbYesNo + vbQuestion, "Flag item") = vbYes Then
                Dim property As UserProperty
                Set property = Item.UserProperties.Add("FlagForFollowUp", olYesNo)
                property.Value = True
            End If
        End If
    End Sub