Sure. That's very easy:
Sub MarkA()
Call MarkWithCategory("A")
End Sub
Sub MarkB()
Call MarkWithCategory("B")
End Sub
Sub MarkWithCategory(strCat As String)
Dim objItem As Object
Set objItem = GetCurrentItem()
If Not objItem Is Nothing Then
objItem.Categories = objItem.Categories & "," & strCat
objItem.Save
End If
Set objItem = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = CreateObject("Outlook.Application")
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
Case Else
' anything else will result in an error, which is
' why we have the error handler above
End Select
Set objApp = Nothing
End Function
You can add MarkA and MarkB to the toolbar by using the View | Toolbars | Customize command and dragging the procedure names from the Macros list on the Commands tab to your toolbar.
Notes on the code:
1) MarkA and MarkB both call the same MarkWithCategory procedure. Using this kind of reusable code you can make as many individual category macros as you like.
2) The code always appends the new category to the existing Categories list. It doesn't check to see if the category already appears in the Categories property for the item. There doesn't seem to be any harm in having a category listed twice, but a refinement would be to check the existing Categories value to see whether a category is already listed.
3) The objItem.Categories = ... statement assumes that the user is in North America or another area of the world where the comma is the default list separator. If that's not the case, you'd need to replace the comma with the semicolon or other separator.
4) GetCurrentItem() is a function that returns either an item selected in a folder view or an individual open item. This means you can use the MarkA and MarkB procedures from either kind of window.
See
http://www.slipstick.com/dev/vb.htm for more Outlook macro basics. I recommend that you set your macro security in Outlook to Medium unless you've digitally signed the VBA project.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at
http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers