Consulting

Results 1 to 3 of 3

Thread: Changing categories of mail items

  1. #1
    VBAX Newbie
    Joined
    Aug 2010
    Posts
    2
    Location

    Changing categories of mail items

    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:

    [VBA] 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

    [/VBA]

  2. #2
    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

  3. #3
    VBAX Newbie
    Joined
    Aug 2010
    Posts
    2
    Location
    Thanks a lot, that seems to have done the trick!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •