We help IT Professionals succeed at work.

outlook vba to clear categories

redpoppy
redpoppy asked
on
3,041 Views
1 Endorsement
Last Modified: 2014-05-10
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

Comment
Watch Question

Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
Stand by....
Retired Senior Systems Analyst/Programmer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
http://screencast.com/t/1yR8o3g3Bsh  Categories are still in the right click optionsThey're still there you might want to customize them
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
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:

 
Select the items for which you want the "Category" removed
Select the macro to run
Macro has run successfuly, only changing selected items
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
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 :-)
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
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!)  :-)

Author

Commented:
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

Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
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
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
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.
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
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?
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
Look at my last comment....are you sure the macros are stored in the main module called "ThisOutlookSession"?
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
Follow these two steps to make sure your macro is in the correct place in your code.
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
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).  :-)

Author

Commented:
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.
Chris RaisinRetired Senior Systems Analyst/Programmer
CERTIFIED EXPERT

Commented:
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! :-)

Author

Commented:
Hi Chris, yes, took your suggestion and just using the one macro. Thanks again for helping me out. Steve
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.