SearchBox Week, Day 2
December 16, 2008 • JP • No Comments • Rate This Article![]()
The search term I chose today was "flag messages", so here's some code to work programmatically with flags in Outlook.
The FlagIcon Property of the MailItem can be used to programmatically set or change the colored flag icon that appears in the Flag Status column in your Explorer Window.
If you don't have the Flag Status column, go to View » Arrange By » Custom…, click on the "Fields" button, find "Flag Status" in the listbox on the left and add it to the listbox on the right.
If you want to read a description of the FlagIcon Property, MSDN has a decent writeup: FlagIcon Property information from Microsoft. But I would avoid using the sample code found there; as I explained in
The Wrong Way to Declare Application Objects, you should never use CreateObject when setting an object reference to an application's native Application Object.
Here's some simple event code that check the default Inbox for new messages. If the message is from certain select people, a flag is applied to the message. All other messages have the purple Flag icon applied. Paste the following code into your ThisOutlookSession module.
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
' set object/event reference to default Inbox
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set myItems = olNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub MyItems_ItemAdd(ByVal Item As Object)
' whenever new items are added to Inbox
Dim Msg As Outlook.MailItem
If TypeName(Item) = "MailItem" Then
Set Msg = Item
Else
GoTo ExitProc
End If
With Msg
Select Case .SenderName
Case "John Smith"
.FlagIcon = olBlueFlagIcon
Case "Mark Johnson"
.FlagIcon = olGreenFlagIcon
Case "My boss' name"
.FlagIcon = olRedFlagIcon
Case Else
.FlagIcon = olPurpleFlagIcon
End Select
.Save
End With
ExitProc:
Set Msg = Nothing
End Sub
The purpose of this exercise is just to make the colored flags appear automatically; to make it actually useful, you can customize your Inbox view to sort messages by Flag by going to View » Arrange By » Flag and watch your incoming emails get auto-sorted based on the criteria you set up in the event code!
The following sub will produce a subsequent count of the flags you've created.
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim myItems As Outlook.Items
Dim Msg As Outlook.MailItem
Dim BlueFlagCount As Long
Dim GreenFlagCount As Long
Dim RedFlagCount As Long
Dim PurpleFlagCount As Long
Dim text As String
Const TITLE As String = "Flag Count"
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set myItems = olNS.GetDefaultFolder(olFolderInbox).Items
If myItems.Count = 0 Then GoTo ExitProc
For Each Msg In myItems
With Msg
If .FlagIcon = olBlueFlagIcon Then BlueFlagCount = BlueFlagCount + 1
If .FlagIcon = olGreenFlagIcon Then GreenFlagCount = GreenFlagCount + 1
If .FlagIcon = olRedFlagIcon Then RedFlagCount = RedFlagCount + 1
If .FlagIcon = olPurpleFlagIcon Then PurpleFlagCount = PurpleFlagCount + 1
End With
Next Msg
text = "There are " & myItems.Count & " emails in your Inbox." & vbCr
text = text & "There are " & BlueFlagCount & " message(s) from John Smith." & vbCr
text = text & "There are " & GreenFlagCount & " message(s) from Mark Johnson." & vbCr
text = text & "There are " & RedFlagCount & " message(s) from your boss." & vbCr
text = text & "There are " & PurpleFlagCount & " message(s) from everyone else."
MsgBox text, , TITLE
ExitProc:
Set myItems = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
Previous Post: SearchBox Week, Day 1
Next Post: SearchBox Week, Day 3



Speak Your Mind
Tell us what you're thinking...Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].