Link to home
Start Free TrialLog in
Avatar of redpoppy
redpoppy

asked on

outlook vba to clear categories

Hi Experts,

I've just upgraded to Outlook 2013 only to find that the quick click for assigning/removing categories is no longer available in the view that I want to use. I found the following code which I'm using to remove categories from a single email, can this be modified to remove the categories from all selected emails ? Thanks in anticipation of your help.

Public Sub ClearCategory()
On Error Resume Next
Dim Item As Object 
Set Item = Application.ActiveExplorer.Selection.Item(1) 
Item.Categories = ""
Item.Save 
End Sub

Open in new window

Avatar of Chris Raisin
Chris Raisin
Flag of Australia image

Stand by....
ASKER CERTIFIED SOLUTION
Avatar of Chris Raisin
Chris Raisin
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
http://screencast.com/t/1yR8o3g3Bsh  Categories are still in the right click optionsUser generated image
You need to look at the Categories column in the loisting of emails.

What you show in the screen capture is a listing for when you want to assign categies to the currently selected item.

It worked fine on my system as showing here:

 
User generated image
User generated image
User generated image
Perhaps you were thinking that when you right-click an item and select categorize that the listing showing is all the categories to which the email belongs?

That is not the case. Right-clicking and then selecting "Categorize" allows you to add a category to the current item's list of categories, clear a category from the items list or categories or even clear ALL categories for the currently selected item. The listing which is showing is an offering of available categories, not the categories currently assigned to that selected item.

To ascertain the categories assigned to an item , look at the mail listing and concentrate on the column "Categories".
If this column is not showing, right click at the top one of the columns and add the column "Categories" to the list of columns you want showing.

Hope this clears this up :-)
Oops...I bacame confused between  David Johnsons comment and the authors (I thought the comment was by the author).

Anyway, the macro does a clearing on each selected item as requested via aa macro.
Of course you should still be able to do this now that David has pointed out th "Right-Click" still works, but at least you now have the code you requested. (Interesting exercise!)  :-)
Avatar of redpoppy
redpoppy

ASKER

Hi, Thanks for looking at this. I'm trying craisin's solution which looks like it should do what I want but suddenly I can't get macros to run on Outlook.

I've added the module to the project, compiled and saved it, so that I now have the two macros "ClearCatAllSel" and the original which I renamed to "ClearCat" - both shown below.

 I added both of these to the quick access toolbar but when I try either of them nothing happens. I also tried adding a new tab "Macros" and put both of them under that tab, but again nothing happens to the message when I select either of them. I've tried putting displays in to see what's happening, but it seems that the routines are just not being executed when selected either from the the QAT or the new tab.

Any suggestions please?

Public Sub ClearCatAllSel()
On Error GoTo ClearCatAllSel_Error
Dim oItem As Object
Dim nCount As Integer
Dim nItem As Integer
Dim objSelection As Outlook.Selection
    Set objSelection = Application.ActiveExplorer.Selection
    nCount = objSelection.Count
    For nItem = 1 To nCount
       Set oItem = objSelection.Item(nItem)
       oItem.Categories = ""
       oItem.Save
    Next
    On Error GoTo 0
    Exit Sub
ClearCatAllSel_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ClearCatAllSel of Module Module1"
End Sub

Open in new window

Public Sub ClearCat()
On Error GoTo ClearCat_Error
Dim Item As Object
    Set Item = Application.ActiveExplorer.Selection.Item(1)
    Item.Categories = ""
    Item.Save
    On Error GoTo 0
    Exit Sub
ClearCat_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ClearCat of Module Module2"
End Sub

Open in new window

Remove the "On error" commands

What is happening is that an error is happening and you have told the macro to ignore the errors an skip to the next statement.
Avoid putting "On Error" commands unless you definately know you want them there (for example when attempting to open a file which may not be there).

On Error commands are used sparingly (in fact I avoid them all the time). There are better ways to handles errors.

Let me know what error message you get.

Cheers
Chris
Now that I look at them, there is no error.

I still recommend you remove the error trapping (there shouldn't be any).

You only really need the "ClearCatAllSel()" macro since both macros do the same thing.

The "ClearCat()" macro will only clear the FIRST selected item (whether it has a category or not) whereas the "ClearCatAllSel" will clear all items that are selected (so if you have only selected one, it will only do that one)....yes they are in fact doing the same thing, so you can do away with the "CleatCat()" macro completely and remove its button.

It works fine on my system. I was thinking some sort of "refresh" might be needed, but I cannot find one (yet)....I'll keep looking while you test some more.
Oh, by the way.

Make sure your macro is stored in the module "This OutlookSession" otherwise it will not run (this is probably your problem).

Only store functions or routines outside of "ThisOutlookSession" when these functions or routines are called from macros already inside the "ThisOutlookSession" module (or external to this main modiule).

All button macros  will look in "ThisOutlookSession" for their macros unless you direct them to look elswhere. Your buttons are lookjing in "This OutlookSession" and I bet your macros are not stored there.

Let me know how you get on.
Hi Chris, Thanks for responding again. I've removed the error trapping and tried again but the macros just don't seem to be running. Yesterday I installed Office 2013 SP1 and I'm wondering if it might have changed some settings that are not allowing macros? I also installed "Trusteer" at the suggestion of my bank and am wondering if that might have something to do with blocking potential security threats - I've stopped it now but it hasn't made any difference. Any ideas?
Look at my last comment....are you sure the macros are stored in the main module called "ThisOutlookSession"?
Yes, calso heck that your securioty allows macros to run (in case it has been changed by somehthing).

I usually allow all macros, but then again I am retired and I trust all emails I receive (if I know the people) and I do not work on any employer machines (so security is not so strict).  :-)
Thanks a lot Chris for your help on this, much appreciated. I re-installed the module as per your instructions and it's working perfectly now.
Great.

Are you just using the one macro now (which covers all scenarios)?
It's a good ideato keep macro buttons to a minimum, something we can do by making macros as "all encompassing" as possible.
Glad all is OK now! :-)
Hi Chris, yes, took your suggestion and just using the one macro. Thanks again for helping me out. Steve