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

OutlookVisual Basic Classic

Avatar of undefined
Last Comment
redpoppy

8/22/2022 - Mon
Chris Raisin

Stand by....
ASKER CERTIFIED SOLUTION
Chris Raisin

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
David Johnson, CD

http://screencast.com/t/1yR8o3g3Bsh  Categories are still in the right click optionsThey're still there you might want to customize them
Chris Raisin

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
Chris Raisin

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 Raisin

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Chris Raisin

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 Raisin

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 Raisin

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
redpoppy

ASKER
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 Raisin

Look at my last comment....are you sure the macros are stored in the main module called "ThisOutlookSession"?
Chris Raisin

Follow these two steps to make sure your macro is in the correct place in your code.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Chris Raisin

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

ASKER
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 Raisin

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! :-)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
redpoppy

ASKER
Hi Chris, yes, took your suggestion and just using the one macro. Thanks again for helping me out. Steve