Outlook 2010 - Set the category based on category of other emails in same conversatio

Status
Not open for further replies.

Kawkaz

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
I'm using Outlook 2010, and trying to use the Categories feature of it. So, I have hundreds of emails in my Inbox, and I've assigned categories for them, and that's fine.My question is: When I send/receive emails which are in the same conversation as mails which I already categorized, how can I make sure Outlook automatically assign the same category to the new mail?For example, I categorized an email (with the subject "Hi dude") as "Friends". Now, I reply back to that mail, and receive an answer from my friend later. I want Outlook to automatically assign my sent mail and the reply the "Friends" category.Is there a VBA for that? Any other option?
 
If you use the conversation view and categorize the conversation header, messages should be categorized automatically. With single messages, you just need to add a category. If you have multiple messages in a thread, you need to select the conversation header above the first message and add the category to it. This obviously works only on messages you receive - if you want to categorize messages you send, you need to use a macro or set the category later. (There is a macro in this article http://www.slipstick.com/outlook/outlook-categories-and-color-categories/ ).
 
Thanks Diane.
I tried to set a category to the header of a conversation.
Still, when I got a new mail in that conversation it does not get the category automatically.
 
Hi Kawkaz,
did you manage to find out why it didn't work ? I have a similar issue with macro-assigned categories. The categories are populated throughout conversations when I manually assign categories, but not when I assign a category via my macro.

It basically works with the first message but does not assign the categories on new messages with same subject shown in the conversation view.

Just curious, whether you found any solution to this ?

Thanks !
 
The macro assigns categories to the actual message, not the conversation, but manually assigning the category when you have a single message puts it on the conversation. Manually assigning to one message in a thread applies only to that message - you need to select the category.

I don't know if you can use a macro to assign a category to the conversation - i never looked into it.
 
Thanks Diane,
can you do me a favour and you post the simple code for assigning a macro to an email. I copy/pasted from various sources, as I was lookign for a short solution since I am workign with some 30/40 categories and equivalent as many custom buttons in the ribbon. It is highly likely that the macro is scrambled and hence not working properly.

The macro I am looking for should be able to assing a category and remove it by hitting the button again incase a category is assigned inccorectly. There can be multiple categrories for each email.

Thanks very much for your help.
harmi
 
At the very basic level, it's as simple as this -

Code:
Public Sub SetCategory()
'by Michael Bauer
'http://www.vboffice.net/en/developers/assign-email-categories-before-sending
  Dim Mail As Object
  Set Mail = Application.ActiveExplorer.Selection.Item(1)
  Mail.Categories = "My Category"
  Mail.Save
 
End Sub


If you want buttons for a bunch of categories, use this method:
Code:
Dim StrCat As String
Sub MyBlue()
StrCat = "My Blue Category"
SetCategory
End Sub

Sub myGreen()
StrCat = "My Green Category"
SetCategory
End Sub

Private Sub SetCategory()
'by Michael Bauer
'http://www.vboffice.net/en/developers/assign-email-categories-before-sending
 
  Dim Mail As Object
  Set Mail = Application.ActiveExplorer.Selection.Item(1)
  Mail.Categories = StrCat
  Mail.Save
  
End Sub

The codes above replace the category, if you want to add categories and keep existing ones, you need to use this format:
Mail.Categories = StrCat & "; " & Mail.Categories
[DOUBLEPOST=1420494048][/DOUBLEPOST]Oh, and if you want to use the category picker, see Michael's original code.
 
Thanks very much Diane,
this is very helpful. How can I amend the macro so it removes the category when you execute the same macro again ?
As an example: running myBlue and myGreen adds the relevant categories, but when I run myGreen again it adds the category once more, rather than removing it ?

Thanks again for your help.
Harmi
 
you need check to see if the name exists, if so, remove it and if not, add it. Removing the category and not calling mail.save is working ok here - if the remove is not sticking for you, add a mail.save before the exit sub.


Code:
Private Sub SetCategory()
  Dim Mail As Object
  Set Mail = Application.ActiveExplorer.Selection.Item(1)
 
Debug.Print Mail.Categories
arr = Split(Mail.Categories, ",")
    If UBound(arr) >= 0 Then

' Check for Category
    For i = 0 To UBound(arr)
      If Trim(arr(i)) = StrCat Then
        ' remove it
            arr(i) = ""
            Mail.Categories = Join(arr, ",")
        ' Category Removed, exit
            Exit Sub
        End If
        Next
    End If
   
' Category not found, add it
    Mail.Categories = StrCat & "," & Mail.Categories

Mail.Save

End Sub
 
Hi Diane,
thanks very much, but it still seems to struggle with categorising new items to a conversation.
I am not sure whether it’s the macro or a hiccup in Outlook.
I was looking around and found below example on the DevCenter, which does seem to work, but lacking the behaviour I am looking for.
Can you help me adding the functionalities such as ‘removal of the same category if already present’ and assigning of categories in the short way similar as you have done it before.
Sub DemoSetAlwaysAssignCategories()
Dim oMail As Outlook.MailItem
Dim oConv As Outlook.Conversation
Dim oStore As Outlook.Store
Set oMail = ActiveExplorer.Selection(1)
Set oStore = oMail.Parent.Store
If oStore.IsConversationEnabled Then
Set oConv = oMail.GetConversation
If Not (oConv Is Nothing) Then
Dim oFolder As Outlook.folder
oConv.SetAlwaysAssignCategories "Best Practices; OOM", oStore
End If
End If
End Sub
Your help is highly appreciated as usual !
 
Try adding The demo macro to the same module as the other then add the demo sub name after the category is set. That should add it to the conversation. I'm not sure if removing the category will remove it from the conversation. Need to investigate it more.
' Category not found, add it
Mail.Categories = StrCat & "," & Mail.Categories
DemoSetAlwaysAssignCategories
 
Hi Diane,
apologies, but I can't seem to figure it out. Can I ask for your help once more ?
I can't get the macro to work properly and not sure whether you managed to find a way to remove the categories again.
Thanks !
 
This is what I tested - I haven't had a chance to test it to see if it removes the category from the conversation too or how to do it, if it doesn't remove it - I had too many meetings scheduled this week.

Code:
 Sub MyBlue()
StrCat = "My Blue Category"
SetCategory
End Sub

Sub myGreen()
StrCat = "My Green Category"
SetCategory
End Sub
Private Sub SetCategory()
   Dim Mail As Object
   Set Mail = Application.ActiveExplorer.Selection.Item(1)
 
Debug.Print Mail.Categories
arr = Split(Mail.Categories, ",")
     If UBound(arr) >= 0 Then

' Check for Category
     For i = 0 To UBound(arr)
       If Trim(arr(i)) = StrCat Then
         ' remove it
             arr(i) = ""
             Mail.Categories = Join(arr, ",")
         ' Category Removed, exit
             Exit Sub
         End If
         Next
     End If
   
' Category not found, add it
     Mail.Categories = StrCat & "," & Mail.Categories
    DemoSetAlwaysAssignCategories

Mail.Save

End Sub
Sub DemoSetAlwaysAssignCategories()
Dim oMail As Outlook.MailItem
Dim oConv As Outlook.Conversation
Dim oStore As Outlook.Store
' Get the item displayed in the Reading Pane.
Set oMail = ActiveExplorer.Selection(1)
Set oStore = oMail.Parent.Store
If oStore.IsConversationEnabled Then
    Set oConv = oMail.GetConversation
    If Not (oConv Is Nothing) Then
        Dim oFolder As Outlook.Folder
        oConv.SetAlwaysAssignCategories StrCat, oStore
    End If
End If
End Sub
 
Hi Diane,

I'm new to VBA and have tried you example above and it works great in my mail inbox.
We are Group of persons working in a shared mailbox and I want to adapt above sample code to the shared mailbox but it doesn't work.
I have tried different ways by changing the code but I can't make it work, what do I need to change in above code to make it working?
I just need the incoming mails in our shared inbox to have the same categorys 1-N as the initial mail in the category.

Thank you in advance!
 
This line:
Set Mail = Application.ActiveExplorer.Selection.Item(1)
tells it to work with the selected message, so as written, it will work with the shared mailbox, but only if you select a message.

if you want it to run automatically on new messages as they arrive, you need to use an itemadd macro and set the folder to watch. Instructions are at How to use an ItemAdd Macro


Remove the set mail line from the macro above and change the name of the macro to
Private Sub objItems_ItemAdd(ByVal Mail As Object)
' macro code here

End Sub


You;ll need to use the code at Working with VBA and non-default Outlook Folders to watch a shared mailbox folder.
 
Hi Diane,

Thank you very much for helping me.

I have tried below code but I get an Array Index Out of Bounds error when a new message arrives in the shared Inbox.
It looks like following row is causing the error : Set oMail = ActiveExplorer.Selection(1)
Could you perhaps instruct me how I can solve get around this error?

I used following code:


Public WithEvents objItems As Outlook.Items

Sub MyBlue()
StrCat = "Erdal"
Application_Startup
End Sub

Private Sub Application_Startup()
Dim myRecipient As Recipient
Dim objNameSpace As NameSpace
Dim inbox As Folder
Set objNameSpace = Application.GetNamespace("MAPI")
Set myRecipient = objNameSpace.CreateRecipient("shared@mail.com")
myRecipient.Resolve
If myRecipient.Resolved Then
Set inbox = objNameSpace.GetSharedDefaultFolder(myRecipient, olFolderInbox)
Set objItems = inbox.Items
End If
End Sub

' Private Sub SetCategory()
Private Sub objItems_ItemAdd(ByVal Mail As Object)

' Dim Mail As Object
' Set Mail = Application.ActiveExplorer.Selection.Item(1)
Dim NS As Outlook.NameSpace
Dim objOwner As Outlook.Recipient

Set NS = Application.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("shared@mail.com")
objOwner.Resolve

If objOwner.Resolved Then
'MsgBox objOwner.Name
Set newINFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
End If

Debug.Print Mail.Categories
arr = Split(Mail.Categories, ",")
If UBound(arr) >= 0 Then

' Check for Category
For i = 0 To UBound(arr)
If Trim(arr(i)) = StrCat Then
' remove it
arr(i) = ""
Mail.Categories = Join(arr, ",")
' Category Removed, exit
Exit Sub
End If
Next
End If

' Category not found, add it
Mail.Categories = StrCat & "," & Mail.Categories
DemoSetAlwaysAssignCategories

Mail.Save

End Sub
Sub DemoSetAlwaysAssignCategories()
Dim oMail As Outlook.mailItem
Dim oConv As Outlook.Conversation
Dim oStore As Outlook.Store
' Get the item displayed in the Reading Pane.
Set oMail = ActiveExplorer.Selection(1)
Set oStore = oMail.Parent.Store

If oStore.IsConversationEnabled Then
Set oConv = oMail.GetConversation
If Not (oConv Is Nothing) Then
Dim oFolder As Outlook.Folder
oConv.SetAlwaysAssignCategories StrCat, oStore
End If
End If
End Sub


As you might have noticed above I have pointed to the Namspace and object owner twice, not sure if it's correct way to doing it.

In addition I don't want the macro to remove previous marked Categorys, just continue marking the same category as previous marked category in the conversation.
Do I need to Always have the "Show as conversation" toggled on in Outlook to have the macro working?

Thank you very much in advance.
 
>> It looks like following row is causing the error : Set oMail = ActiveExplorer.Selection(1)
You don't use selection with item add macro. ?The message is the one just added, identified by Mail in objItems_ItemAdd(ByVal Mail As Object)


Since you need to reuse the Inbox, declare it globally so you don't need to keep finding it. I'm changing it to olInbox, just because its better form. I also cleaned out some of the comments out code so its easier to read.

This is working here - when a new items is added to the Inbox, the category Erdal is added.



Code:
Public WithEvents objItems As Outlook.items
Dim olInbox As Outlook.Folder

Private Sub Application_Startup()
Dim myRecipient As Recipient
Dim objNameSpace As NameSpace
Set objNameSpace = Application.GetNamespace("MAPI")
Set myRecipient = objNameSpace.CreateRecipient("shared@mail.com")
myRecipient.Resolve
If myRecipient.Resolved Then
'Set olInbox = objNameSpace.GetSharedDefaultFolder(myRecipient, olFolderInbox)
Set olInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
Set objItems = olInbox.items
End If
End Sub

Private Sub objItems_ItemAdd(ByVal Mail As Object)
StrCat = "Erdal"
Debug.Print Mail.Categories
arr = Split(Mail.Categories, ",")
If UBound(arr) >= 0 Then
' Check for Category
For i = 0 To UBound(arr)
If Trim(arr(i)) = StrCat Then
' remove it
arr(i) = ""
Mail.Categories = Join(arr, ",")
' Category Removed, exit
Exit Sub
End If
Next
End If
' Category not found, add it
Mail.Categories = StrCat & "," & Mail.Categories
DemoSetAlwaysAssignCategories Mail
Mail.Save

End Sub

Sub DemoSetAlwaysAssignCategories(ByVal oMail As Object)
Dim oConv As Outlook.Conversation
Dim oStore As Outlook.Store
' Get the item displayed in the Reading Pane.
Set oStore = oMail.Parent.Store
If oStore.IsConversationEnabled Then
Set oConv = oMail.GetConversation
If Not (oConv Is Nothing) Then
Dim oFolder As Outlook.Folder
oConv.SetAlwaysAssignCategories StrCat, oStore
End If
End If
End Sub
 
Hi Diane,

Again thank you very much. I tried the code in my end, but I get Another exception, please check enclosed image.
1524772656621.png


The debug info is pointing out following row as the cause of the error: oConv.SetAlwaysAssignCategories StrCat, oStore.
Does this mean I am not allowed to call SetAlwaysAssignCategories in my Outlook version?
I'm Using following version:

1524773274684.png


Thank you in advance.
 
That exact macro won't work in online mode, but it can be made to work.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P problem with set up group by conversation in outlook 2010 Using Outlook 3
C Set reminder time in custom form (Outlook 2010) Using Outlook 1
S How to set Company as default "File As" in Contacts for Outlook 2010 ? Using Outlook 1
B set deafult view in public folder for all users from outlook 2010 64bit Using Outlook 0
T Outlook 2010 Mutliple Exchange Accounts SET UP QUESTIONS Using Outlook 4
F Outlook 2010 - Default font color keeps changing frim the one I set. Using Outlook 1
S Adding a recipient's column to Sent folder in Outlook 2010 Outlook VBA and Custom Forms 1
e_a_g_l_e_p_i Can emails from Gmail be deleted when they are downloaded to Outlook 2010 Using Outlook 1
L Outlook 2010 Outlook 2010 Outlook VBA and Custom Forms 2
C What folders are needed when reinstalling Outlook 2010 Using Outlook 0
e_a_g_l_e_p_i Gmail in Outlook 2010 preview issue Using Outlook 4
e_a_g_l_e_p_i Outlook 2010 Help setting up Gmail account in Outlook 2010 Using Outlook 3
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
M Outlook 2010 Outlook 2010 with O365 / Exchange Online Using Outlook 0
F Outlook 2010 Outlook 2010 and GMail Using Outlook 0
D Outlook 2007 vs. Outlook 2010 -- ToDo Bar Using Outlook 0
e_a_g_l_e_p_i I think it may be time to upgrade from Outlook 2010 Using Outlook 3
T Why does outlook 2010 convert only some forum notifications to plain text? Using Outlook 0
I Error saving screenshots in a custom form in outlook 2016, outlook 365 - ok in outlook 2013, outlook 2010 Outlook VBA and Custom Forms 5
M Outlook 2010 Problem with OutLook 2010 32 bit, after Windows Auto Update Using Outlook 3
M PST import from Outlook 2007 to 2010 - Address Book contacts all in 1 group Using Outlook 4
D Outlook 2010 account setup fails in particular domain Using Outlook 3
B Outlook 2010 is Auto Purging when not configured for that Using Outlook 1
W Outlook 2010 Reading Pane Slows Startup Using Outlook 3
S Outlook 2010 unable to change default font Using Outlook 7
B Outlook 2010 Can not find a certain file in M/S Outlook 2010. Using Outlook 1
Mark Foley Cannot enable add-in in outlook 2010 Using Outlook 0
W Outlook 2010 some sent items marked unread now (was Ok before) Using Outlook 0
RBLampert Updating from Outlook 2010 to Outlook 365 Using Outlook 0
L What are the risks of opening an Outlook 2016 .pst file in Outlook 2010? Using Outlook 4
S Unable to remove rule outlook 2010 Using Outlook 0
N Outlook 2010 Flag blocked for Safe Senders List???? Using Outlook 7
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
Mark Foley Unable to subscribe to published calendar in Outlook 2010 Using Outlook 4
K Maximum Categorize Shortcuts In Outlook 2010? Using Outlook 1
E Unable to open Outlook 2010 after adding new email account Using Outlook 4
RBLampert Outlook 2010 no longer (?) shrinks large images Using Outlook 4
N Outlook 2010 will not send nor receive Using Outlook 4
U Outlook 2010 'freezes' before moving emails Using Outlook 2
P Outlook 2010 trusted emails going to spam folder Using Outlook 18
E Outlook 2010 Subject sort uses Thread-Topic for grouping Using Outlook 2
King Mustard Maximum Categorize shortcuts in Outlook 2010? Using Outlook 1
C Outlook 2010 keeps asking for username and password Using Outlook 1
M Duplicate Primary Mail Accounts outlook 2010 Using Outlook 0
T Compacting Outlook 2010 OST results in old emails being re-sent Using Outlook 6
K Outlook 2010 duplicate download emails 1 inbox 1 PST no updates Using Outlook 3
D create an html table in outlook custom form 2010 using vba in MsAccess Outlook VBA and Custom Forms 7
Potty Ash MS Outlook 2010 custom form - validation or formula to request user to check a checkbox Outlook VBA and Custom Forms 16
RBLampert Outlook 2010 sends to/receives from some Gmail addresses but not others Using Outlook 3
e_a_g_l_e_p_i Question about address book in Outlook 2010 Using Outlook 9

Similar threads

Back
Top