Assigning msg category with a button

W

Wowbagger

Is it possible to place a button in the toolbar that will assign a specific
message category with a single click?

For example, say that I have two buttons: clicking button one would assign a
message category of A, clicking button two would assign category B and
clicking both would assign both categories.

Can this be done in Outlook 2000 or later?

Thanks
 
S

Sue Mosher [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top