• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

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

Kawkaz

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#1
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?
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#2
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/ ).
 

Kawkaz

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#3
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.
 

Harmi

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#4
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 !
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#5
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.
 

Harmi

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#6
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#7
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.
 

Harmi

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#8
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#9
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
 

Harmi

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#10
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 !
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#12
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
 

Harmi

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#13
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 !
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#14
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
 

erdale

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Outlook.com (as MS Exchange)
#15
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!
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#16
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.
 

erdale

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Outlook.com (as MS Exchange)
#17
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.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#18
>> 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
 

erdale

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Outlook.com (as MS Exchange)
#19
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.
 

Similar threads