VBA code to remove flag from all messages in Inbox

M

Murphybp2

I flag certain messages that I get with a certain color flag. I would
like to create a button that could remove that color flag from those
messages all at once. The end result would be the messages would no
longer have a flag. How would I accomplish this?
 
M

Michael Bauer

Am 23 Apr 2006 11:36:07 -0700 schrieb (e-mail address removed):

This sample deletes the text and icon. You only need to write yourself the
constant for the FlagIcon that you´re looking for. Simply place the mouse at
the end of the line "If Mail.FlagIcon=", right click and chose "show
constants" (or something similiar).

Public Sub UnflagItems()
Dim Items As Outlook.Items
Dim Mail as Outlook.MailItem
Dim obj as Object
Set Items = Application.Session.GetDefaultFolder(olFolderInbox).Items
For Each obj in Items
If Typeof Item is Outlook.MailItem Then
Set Mail=Item
If Mail.FlagIcon=
Mail.FlagIcon=olNoFlagIcon
Mail.FlagRequest=vbNullString
Mail.Save
Endif
Endif
Next
End Sub
 
M

Murphybp2

I tried this code but I am getting an error for the line that states"If
Mail.FlagIcon=olYellowFlagIcon"

Public Sub RemoveYellowFlagFromMessages()

Dim Items As Outlook.Items
Dim Mail As Outlook.MailItem
Dim obj As Object
Set Items = Application.Session.GetDefaultFolder(olFolderInbox).Items
For Each obj In Items
If TypeOf Item Is Outlook.MailItem Then
Set Mail = Item
If Mail.FlagIcon=olYellowFlagIcon
Mail.FlagIcon = olNoFlagIcon
Mail.FlagRequest = vbNullString
Mail.Save
End If
End If
Next
End Sub
 
M

Michael Bauer

Am 2 May 2006 09:02:06 -0700 schrieb (e-mail address removed):

Simply add a "then" at the end of that line. The syntax is: If ... Then ...
 
M

Murphybp2

Thanks. I added that and tried it again. I don't get an error now,
but nothing seems to happen. I have 10 messages in my inbox with a
yellow flag. When I ran the macro, I expected it to clear the flag
from those items, but nothing happened. Here is the code I have. Do I
need to change anything else? I even tried selecting the messages
first, but that didn't help.

Public Sub RemoveYellowFlagFromMessages()

Dim Items As Outlook.Items
Dim Mail As Outlook.MailItem
Dim obj As Object
Set Items = Application.Session.GetDefaultFolder(olFolderInbox).Items
For Each obj In Items
If TypeOf Item Is Outlook.MailItem Then
Set Mail = Item
If Mail.FlagIcon = olYellowFlagIcon Then
Mail.FlagIcon = olNoFlagIcon
Mail.FlagRequest = vbNullString
Mail.Save
End If
End If
Next
End Sub
 
M

Michael Bauer

Am 3 May 2006 17:08:24 -0700 schrieb (e-mail address removed):
For Each obj In Items
If TypeOf Item Is Outlook.MailItem Then
Set Mail = Item

"Item" is not a declared variable. Replace it please by "obj".
 
M

Murphybp2

I made that change.....still not working. Is there something else I'm
missing?

Public Sub RemoveYellowFlagFromMessages()

Dim Items As Outlook.Items
Dim Mail As Outlook.MailItem
Dim obj As Object
Set Items = Application.Session.GetDefaultFolder(olFolderInbox).Items
For Each obj In Items
If TypeOf Item Is Outlook.MailItem Then
Set Mail = obj
If Mail.FlagIcon = olYellowFlagIcon Then
Mail.FlagIcon = olNoFlagIcon
Mail.FlagRequest = vbNullString
Mail.Save
End If
End If
Next
End Sub
 
M

Murphybp2

I'm not understanding what you're saying I should change. I have a
line that states > If TypeOf Item Is Outlook.MailItem Then. What part
do I need to fix? I don't have any skill with Outlook VBA, so I'm
basically looking to just copy and paste the code.
 
S

Sue Mosher [MVP-Outlook]

Reread Michael's earlier post, in which he said:

"Item" is not a declared variable. Replace it please by "obj".

You also need to remove the flag itself:

Mail.FlagStatus = olNoFlag

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
M

Murphybp2

So is this correct now? Or do I need to replace every time that is
says "Items" with "obj". I tried running this code and all it seemed
to do was lock up my machine.

Public Sub RemoveYellowFlagFromMessages()

Dim Items As Outlook.Items
Dim Mail As Outlook.MailItem
Dim obj As Object
Set Items = Application.Session.GetDefaultFolder(olFolderInbox).Items
For Each obj In Items
If TypeOf obj Is Outlook.MailItem Then
Set Mail = obj
If Mail.FlagIcon = olYellowFlagIcon Then
Mail.FlagIcon = olNoFlagIcon
Mail.FlagRequest = vbNullString
Mail.Save
End If
End If
Next
End Sub
 
S

Sue Mosher [MVP-Outlook]

You probably will want to add a statement to reset each item's FlagStatus property to either clear the flag or set it complete, as you prefer.

Otherwise, I don't see anything amiss with this code. Replacing Items with obj would break the whole procedure. (Think about it: What does each variable represent?)

What happens when you step through the code in the debugger?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top