• Outlook User
  • Exchange Admin
  • Office 365
  • Outlook Developer
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
    • Common Problems
    • Outlook BCM
    • Utilities & Addins

How to set a flag to follow up using VBA

Slipstick Systems

› Developer › Code Samples › How to set a flag to follow up using VBA

Last reviewed on August 29, 2018     119 Comments

You can edit the Flag to Follow up field text each time you flag a message, but you can't save the changes permanently, or even temporarily in an MRU list. However, you can use VBA to set flags, which will allow you to save custom Flag to text, as well as custom reminder times and start or due dates.

This code sample sets a flag to "Call display-name", with a due date in 3 days and a reminder set for 2 days.
Set a custom flag for followup

To create the macro, open the VBA editor using Alt+F11. Right click on Project1 and choose Insert > Module. Paste the macro into the module and edit as needed.

You'll also need the GetCurrentItem function from Work with an open or selected Outlook item. Paste it in the module following this macro.

For more information on using VBA, see How to use the VBA Editor.

To use the macro, open or select a message and run the macro.

Public Sub SetCustomFlag()

Dim objMsg As Object

' GetCurrent Item function is athttp://slipstick.me/e8mio
Set objMsg = GetCurrentItem()

With objMsg

' due this week flag
    .MarkAsTask olMarkThisWeek
' sets a specific due date
    .TaskDueDate = Now + 3

    .FlagRequest = "Call " & objMsg.SenderName
    .ReminderSet = True
    .ReminderTime = Now + 2
    .Save
End With

Set objMsg = Nothing
End Sub

Note: if you don't include the .MarkAsTask line, the flag will be the 'people flag'. If you use olMarkToday and set the due date for next week, it's converted to a 'next week' flag.

 

Flag messages after sending

This macro will ask if you want to flag a message after sending it. It waits for a message to hit the Sent folder then asks if you want to flag it.

Because this is an Application_Startup macro, you need to click in Application_Startup code and click Run to test it.

Option Explicit
 
Private WithEvents olSentItems As Items
 
Private Sub Application_Startup()
  Dim objNS As NameSpace
  Set objNS = Application.Session
  ' instantiate objects declared WithEvents
  Set olSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
  Set objNS = Nothing
End Sub
 
Private Sub olSentItems_ItemAdd(ByVal Item As Object)
  On Error Resume Next
  Dim prompt As String

    prompt$ = "Do you want to flag this message for followup?"
       If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Add flag?") = vbYes Then
            
   With Item
    .MarkAsTask olMarkThisWeek
      ' sets a due date in 3 days
    .TaskDueDate = Now + 3
    .ReminderSet = True
    .ReminderTime = Now + 2
    .Save
End With
       End If
End Sub

 

No Message Box: Say Yes Before Sending

This variation of the above macro uses a button on the ribbon to click Yes rather than the message box as the message is sent.

Add the SayYes macro to the message form ribbon and click it before sending any message that you want to flag for follow-up.

Option Explicit
Dim SetFlag
Private WithEvents olSentItems As Items
 
Private Sub Application_Startup()
  Dim objNS As NameSpace
  Set objNS = Application.Session
  ' instantiate objects declared WithEvents
  Set olSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
  Set objNS = Nothing
End Sub
 
Private Sub olSentItems_ItemAdd(ByVal Item As Object)
  On Error Resume Next
  Dim prompt As String
       If SetFlag = vbYes Then
          
   With Item
    .MarkAsTask olMarkThisWeek
      ' sets a due date in 3 days
    .TaskDueDate = Now + 3
    .ReminderSet = True
    .ReminderTime = Now + 2
    .Save
   End With
       End If
 SetFlag = vbNo
End Sub

Sub SayYes()
 SetFlag = vbYes
End Sub

Select a Due Date

The macro samples above use a predefined date for the flag (and/or reminder). If you want to choose your own date, you'll need to use an InputBox and enter either 'a days from now' value or a date (in any valid short date format).

To enter a number for days from now, use this code, with the DIM statement at the top, the InputBox before the With Item line, and replace the TaskDueDate line with the one below.

Dim countDays As Long
countDays = InputBox("How many days from now?")
.TaskDueDate = countDays

To enter a specific date, use this code, with the DIM statement at the top, the InputBox before the With Item line, and replace the TaskDueDate line with the one below. In this code, any valid short date format should work: 4/1, 4/1/17, or 4/1/2017.

Dim RemindMeOn As Date
RemindMeOn = InputBox("Enter the reminder date in any valid short date (m/d/y) format")
.TaskDueDate = RemindMeOn

Flag all messages as they arrive

If you want to flag all messages as they arrive, you can use an ItemAdd macro, or use a rule with a run a script macro. In this example, I have an ItemAdd macro. You could use an If statement to restrict which messages are flagged, or convert it to a run a script macro and use a rule to filter the messages.

To use this code, paste it into ThisOutlookSession, click in the Application_Startup macro and click the Run button. Don't forget to set your macro security to low during testing then use SelfCert to sign it when you are satisfied it works as expected.

Option Explicit
  
Private WithEvents olItem As Items
  
Private Sub Application_Startup()
  Dim objNS As NameSpace
  Set objNS = Application.Session
  ' instantiate objects declared WithEvents
  Set olItem = objNS.GetDefaultFolder(olFolderInbox).Items
  Set objNS = Nothing
End Sub
  
Private Sub olItem_ItemAdd(ByVal Item As Object)
  On Error Resume Next
             
   With Item
    .MarkAsTask olMarkThisWeek
      ' sets a due date in 24 hours
    .TaskDueDate = Now + 1
    .ReminderSet = True
    .ReminderTime = Now + 1
    .Save
End With

End Sub

Run a Script rule to Flag Messages

To use the macro in run a script rule, use the following code sample.

Public Sub FlagMessage(Item As Outlook.MailItem)
  On Error Resume Next
              
   With Item
    .MarkAsTask olMarkThisWeek
      ' sets a due date in 24 hours
    .TaskDueDate = Now + 1
    .ReminderSet = True
    .ReminderTime = Now + 1
    .Save
End With
 
End Sub

See Run a Script rules for more information on using Run a Script rules.

Mark Flags Completed and Remove Categories

Use this macro to mark flagged message complete and remove any categories assigned to the message.

Option Explicit
 
Public Sub DoSomethingSelection()
    Dim Session As Outlook.NameSpace
    Dim currentExplorer As Explorer
    Dim Selection As Selection
     
    Dim obj As MailItem
 
    Set currentExplorer = Application.ActiveExplorer
    Set Selection = currentExplorer.Selection
 
 
    For Each obj In Selection
  
     With obj
      .Categories = ""
      .FlagStatus = olFlagComplete
      .Save
     End With
 
    Next
 
    Set Session = Nothing
    Set currentExplorer = Nothing
    Set obj = Nothing
    Set Selection = Nothing
 
End Sub

Outlook 2003

If you are using Outlook 2003, you need to edit the code a little, as Outlook 2003 uses colored flags.

Allowable Flag colors
olNoFlagIconolPurpleFlagIcon
olOrangeFlagIconolGreenFlagIcon
olYellowFlagIconolBlueFlagIcon
olRedFlagIcon

Public Sub SetCustomFlag()

Dim objMsg As Object

' GetCurrent Item function is athttp://slipstick.me/e8mio
Set objMsg = GetCurrentItem()

With objMsg
    .FlagIcon olPurpleFlagIcon
    .FlagDueBy = Now + 3
    .FlagRequest = "Call " & objMsg.SenderName
    .ReminderSet = True
    .ReminderTime = Now + 2
    .Save
End With

Set objMsg = Nothing
End Sub

How to use macros

First: You need to have macro security set to low during testing. The macros will not work otherwise.

To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

Some macros need to be in ThisOutlookSession, others go into a module or can be placed in either ThisOutlookSession or a module. The instructions are below.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

If you are told to put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

If you are told to put the macro code in ThisOutlookSession:

  1. Expand Project1 and double click on ThisOutlookSession.
  2. Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)

More information as well as screenshots are at How to use the VBA Editor

More Information

  • Automatically create a task when sending a message
  • Create a Task from an Email using a Rule
  • Create an Outlook Appointment from a Message
  • Create Task or Appointment and Insert Selected Text
  • Create Tasks from Email and move to different Task folders
  • Replicate GTD: Create a task after sending a message

How to set a flag to follow up using VBA was last modified: August 29th, 2018 by Diane Poremsky
  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
  • Print

Related Posts:

  • Use this macro to send an attachment to email addresses in the To line
    VBA: No attachments to CC'd recipients
  • Create a Task when a Message is Flagged
  • New message to selected contacts using the BCC field
  • Flag for Follow up the Next Business Day

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
119 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

MynameisNed (@guest_219639)
August 16, 2022 3:49 am
#219639

Hi Diane - is it possible to capture more than one outgoing email? I have a 2 min delay rule in place. If I send two emails within that 2 min period (both to be flagged) the variable is wiped/cleared upon the first hitting sent box so the second doesnt get flagged...

Any thoughts?

0
0
Reply
Josiah Johnson (@guest_218636)
August 16, 2021 3:09 pm
#218636

Hi Diane. Thank you kindly for the code. There is a bit of a bug where it will error if you try to set the TaskDueDate = 1. The TaskStartDate defaults to 2 days out, so you need to first set TaskStartDate to the same thing as your TaskDueDate. Otherwise the due date is before the start date and it errors. That took me a while to figure out, so I wanted to share the fix.

1
0
Reply
Christopher C (@guest_218571)
July 25, 2021 1:10 pm
#218571

Diane,

I've used these macro's for years and appreciate everything you've posted here. I'm trying to get the macro that flags sent emails for follow-up to work with multiple email accounts in outlook, but as you've mentioned before it only works with the defaults.

I've been trying to incorporate the code from the "working with VBA and non-default folders" post and can make it work with the secondary email account, but then it stops functioning with the default account.

Any assistance you can provide is appreciated.

0
0
Reply
Alvin (@guest_217566)
February 13, 2021 7:58 am
#217566

Is there a way to set a flag Orange Category and Flag for follow up just before you send the email. The example above is for after you sent an email.
There is flag 🚩 button and change category just before we press send.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Alvin
February 13, 2021 9:29 am
#217568

It is possible to set a category before sending - this will send it to the recipient too (they may or may not see it, depending on the mail client and if rules or their server removes it).

It's better to set it using a macro you run manually, so you can choose whether to add it.

I have a macro here somewhere that does it - I will update this article with it.

0
0
Reply
Naveen (@guest_217198)
November 18, 2020 2:31 pm
#217198

Hi Diane,

Hope you are doing good:)

I have an situation in outlook any emails which comes in to the share mail inbox i want it to automatically set up a flag for 2 days, i tried using the code which you have written looks like it is not working in shared mail box, can you please help me on this.

Public Sub SetCustomFlag()

Dim objMsg As Object

' GetCurrent Item function is athttp://slipstick.me/e8mio
Set objMsg = GetCurrentItem()

With objMsg

' due this week flag
    .MarkAsTask olMarkThisWeek
' sets a specific due date
    .TaskDueDate = Now + 2

    .FlagRequest = "Call " & objMsg.SenderName
    '.ReminderSet = True
    '.ReminderTime = Now + 2
    .Save
End With

Set objMsg = Nothing
End Sub

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Naveen
February 13, 2021 9:34 am
#217569

The macro in your question should work on any message in any folder in your profile - you need the GetCurrent Item function from http://slipstick.me/e8mio

if its a shared exchange mailbox and you want to do it automatically using an itemadd macro, you need to use the shared mailbox code at
Working with VBA and non-default Outlook Folders (slipstick.com)
(I will add an itemadd version to this page)

0
0
Reply
ASHISH BHALARA (@guest_215828)
September 3, 2020 2:26 am
#215828

I send mail from excel and this code not working in excel VBA.
What code I need for flag in excel vba?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  ASHISH BHALARA
February 13, 2021 7:27 pm
#217570

this the itemsend macros should do it,
https://www.slipstick.com/developer/code-samples/set-flag-follow-up-using-vba/#flag

0
0
Reply
Dicinox (@guest_215530)
June 30, 2020 7:00 pm
#215530

Hi, thanks for this. It is very helpful.

I just have a question, is it possible to automatically set the reminder if the Subject contains a keyword?

For example i send an email and instead of the dialog box, it would set me a reminder if the subject contains "FOLLOW UP"

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Dicinox
June 30, 2020 10:57 pm
#215531

You can do that using a rule - if you have reminders set for tasks with due dates. Or you can use a rule and run a script macro to set the reminder.

2020-06-30_22-56-25-0000.png
0
0
Reply
Emily (@guest_215176)
May 6, 2020 10:26 am
#215176

I've been looking for this code for weeks. Thank you so much for making it so understandable! Outlook VBA seems to have changed drastically over the years, and most answers I have come across is for Outlook 2003!

0
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 28 Issue 11

Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Subscribe to Exchange Messaging Outlook






Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • WeekMonthAll
  • Adjusting Outlook's Zoom Setting in Email
  • How to Remove the Primary Account from Outlook
  • Cannot add Recipients in To, CC, BCC fields on MacOS
  • Move an Outlook Personal Folders .pst File
  • Save Sent Items in Shared Mailbox Sent Items folder
  • Create rules that apply to an entire domain
  • Outlook's Left Navigation Bar
  • Use PowerShell to get a list of Distribution Group members
  • View Shared Calendar Category Colors
  • Remove a password from an Outlook *.pst File
  • Cannot add Recipients in To, CC, BCC fields on MacOS
  • Change Appointment Reminder Sounds
  • Messages appear duplicated in message list
  • Reset the New Outlook Profile
  • Delete Old Calendar Events using VBA
  • Use PowerShell or VBA to get Outlook folder creation date
  • Outlook's Left Navigation Bar
  • Contact's Display Bug
  • Use PowerShell to get a list of Distribution Group members
  • Edit Outlook’s Attach File list
Ajax spinner

Newest Code Samples

Delete Old Calendar Events using VBA

Use PowerShell or VBA to get Outlook folder creation date

Rename Outlook Attachments

Format Images in Outlook Email

Set Outlook Online or Offline using VBScript or PowerShell

List snoozed reminders and snooze-times

Search your Contacts using PowerShell

Filter mail when you are not the only recipient

Add Contact Information to a Task

Process Mail that was Auto Forwarded by a Rule

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in Outlook for Windows.

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Outlook Links Won’t Open In Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook Top Issues

  • The Windows Store Outlook App
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

Outlook-tips.net Samples

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

MSDN Outlook Dev Forum

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Diane Poremsky [Outlook MVP]

Make a donation

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Outlook Suggestion Box (UserVoice)

Slipstick Support Services

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Advertise | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2023 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment