最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vba - How Do I Remove a Flag from an Email in Outlook - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

I 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
发布评论

评论列表(0)

  1. 暂无评论