Log in

View Full Version : Categories Help



Zyhin
07-14-2015, 06:07 PM
Hi all,

I am currently using categories for prioritising my work. Using names 1, 2, 3, 4 and so on to 10. I know it should be possible but due to my lack of knowledge in Outlook and Outlook relevant code. I am wanting to run a macro every time I finish a job so that it will change all categories down one number. eg 2 becomes 1 and 7 becomes 6. Each category could have any number of emails assigned to it.

Thanks in advance.

Z

If possible can you please explain what you have done so I can get closer to understanding what is going on.

skatonni
07-23-2015, 02:18 PM
Try something like this:


Sub Reset_Categories()

Dim fldrItems As Items
Dim itm As Object
Dim i As Long

Set fldrItems = Session.GetDefaultFolder(olFolderInbox).Items

For Each itm In fldrItems
If itm.Categories = "1" Then
itm.Categories = "Complete"
itm.Save
End If
Next itm

For i = 2 To 10
'Debug.Print "Processing old category: " & i

For Each itm In fldrItems
If itm.Categories = CStr(i) Then
'Debug.Print itm.Subject
itm.Categories = CStr(i - 1)
'Debug.Print " - " & i & " changed to " & i - 1
itm.Save
End If
Next itm

Next I

ExitRoutine:
Set fldrItems = Nothing
Set itm = Nothing

'Debug.Print "Done."

End Sub


You can see what is happening by stepping through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

You will note it is inefficient, processing every item 10 times. See here http://www.jpsoftwaretech.com/use-filters-to-speed-up-outlook-macros for a way to target the items with the category you want to update.