I'm trying to remove a flag from an Outlook message, and my code works sometimes but not always. How can I remove a flag from any message?
Here's the code that sometimes does not work:
If Not myMailItem.FlagStatus <> olNoFlag Or myMailItem.FlagRequest <> vbNullString Then
myMailItem.FlagStatus = olNoFlag
myMailItem.FlagRequest = vbNullString
End If
I posted an answer but I'm open to better solutions and improvements.
I'm trying to remove a flag from an Outlook message, and my code works sometimes but not always. How can I remove a flag from any message?
Here's the code that sometimes does not work:
If Not myMailItem.FlagStatus <> olNoFlag Or myMailItem.FlagRequest <> vbNullString Then
myMailItem.FlagStatus = olNoFlag
myMailItem.FlagRequest = vbNullString
End If
I posted an answer but I'm open to better solutions and improvements.
Share Improve this question edited Mar 6 at 18:41 ChrisB asked Mar 6 at 2:09 ChrisBChrisB 3,2415 gold badges40 silver badges68 bronze badges2 Answers
Reset to default 1I had to look at the MailItem.TaskDueDate
and MailItem.ReminderTime
properties. I suspect that when an Outlook rule flagged a message, the rule set these properties but did not set the MailItem.ReminderSet
or MailItem.FlagRequest
properties.
Here's what finally worked for me:
If myMailItem.TaskDueDate <> #1/1/4501# Or myMailItem.ReminderTime <> #1/1/4501# Or myMailItem.ToDoTaskOrdinal <> #1/1/4501# Or myMailItem.IsMarkedAsTask Or myMailItem.ReminderSet Or myMailItem.FlagStatus <> OlFlagStatus.olNoFlag Or myMailItem.FlagRequest <> vbNullString Then
myMailItem.TaskDueDate = #1/1/4501# ' null date is #1/1/4501#
myMailItem.ReminderTime = #1/1/4501# ' null date is #1/1/4501#
myMailItem.ToDoTaskOrdinal = #1/1/4501# ' null date is #1/1/4501#
targetMailItem.ClearTaskFlag ' use this method since IsMarkedAsTask property can't be changed directly
myMailItem.ReminderSet = False
myMailItem.FlagRequest = vbNullString
End If
Your issue comes from the incorrect logic in your If condition and using an incorrect variable (iMailItem instead of myMailItem).
If the flag is not being removed reliably, try the following approach:
Updated Code:
Dim myMailItem As Outlook.MailItem
' Ensure the mail item is valid before proceeding
If Not myMailItem Is Nothing Then
' Remove the flag properly
myMailItem.ClearTaskFlag
' Save changes
myMailItem.Save
End If