PDA

View Full Version : Changing categories of mail items



mrburns
08-27-2010, 02:56 AM
I want to run a macro on selected mail which is marked by a specific category. Once the macro has finished I want to set the category to something different so that the mail is still distinguishable but the macro will not run on that mail twice. I can detect the category fine but when I try change the category usually nothing happens, it will only successfully change the category if I have the mail item open or in the preview pane. Any ideas what the problem could be?
This is the code I am using:

Dim myolApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace
Dim payFolder As MAPIFolder
Dim selectedMail As New Collection

Set myolApp = Application
Set myNamespace = myolApp.GetNamespace("MAPI")
Set payFolder = myNamespace.Folders.Item("Personal Folders")

Dim previousWeek As Date, numMail As Integer, count As Integer
previousWeek = Now - DateValue("08/01/01") + DateValue("01/01/01")
numMail = payFolder.Items.count
count = 0
Do While payFolder.Items(numMail - count).ReceivedTime > previousWeek
If payFolder.Items(numMail - count).Categories = "Red Category" Then
payFolder.Items(numMail - count).Categories = "Green Category"
payFolder.Items(numMail - count).Save
selectedMail.Add payFolder.Items(numMail - count)
End If
count = count + 1
Loop

Set myolApp = Nothing
Set myNamespace = Nothing
Set payFolder = Nothing
Set selectedMail = Nothing

Crocus Crow
08-27-2010, 04:41 AM
Try setting the item to a mailItem object first, like this within your If statement:

Dim email As Outlook.mailItem

If payFolder.Items(numMail - count).Class = olMail Then
Set email = payFolder.Items(numMail - count)
email.Categories = "My category"
email.Save
End If

mrburns
08-27-2010, 08:12 AM
Thanks a lot, that seems to have done the trick!