Microsoft Office Forums create macro to add category to email

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-10-2019, 10:04 PM
melfinster melfinster is offline create macro to add category to email Windows 10 create macro to add category to email Office 2016
Novice
create macro to add category to email
 
Join Date: Jun 2019
Posts: 3
melfinster is on a distinguished road
Default create macro to add category to email

Hello
I want to put a button in the ribbon that assigns a category called "TRIM" to an email. i'd like to do this to an incoming email or an outgoing email. I know I can create a button that runs a macro, could someone please help me write the VBA for the macro
Mel
Reply With Quote
  #2  
Old 06-11-2019, 04:25 AM
gmayor's Avatar
gmayor gmayor is offline create macro to add category to email Windows 10 create macro to add category to email Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,761
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

This is fairly straightforward, however categories are case sensitive, so the following assumes "trim", "Trim" or "TRIM" are all intended to equal "TRIM" and so if they exist "TRIM" is not added again.

Code:
Sub Add_Category()
Dim olItem As Object
Dim Arr As Variant
Dim i As Integer
Const strCat As String = "TRIM"
    On Error GoTo err_Handler
    Set olItem = Application.ActiveExplorer.Selection.Item(1)
    If TypeName(olItem) = "MailItem" Then
        Arr = Split(olItem.Categories, ",")
        If UBound(Arr) >= 0 Then
            For i = 0 To UBound(Arr)
                If Trim(UCase(Arr(i))) = strCat Then
                    Beep
                    MsgBox "Already categorised with " & strCat
                    GoTo lbl_Exit
                End If
            Next
        End If
        olItem.Categories = strCat & "," & olItem.Categories
        olItem.Save
    End If
lbl_Exit:
    Set olItem = Nothing
    Exit Sub
err_Handler:
    Beep
    MsgBox "Select a message!"
    Err.Clear
    GoTo lbl_Exit
End Sub
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 06-11-2019, 03:35 PM
melfinster melfinster is offline create macro to add category to email Windows 10 create macro to add category to email Office 2016
Novice
create macro to add category to email
 
Join Date: Jun 2019
Posts: 3
melfinster is on a distinguished road
Default

Thank you for this. For some reason it's not working for me.

I have a category already labelled 'TRIM'
In VB, I have VbaProject.OTM - ThisOutlookSession with your code

In the Home ribbon, I added a new group and then added your Macro (CategoryButton.ThisOutlookSession.Add_Category) and renamed it.

In the email, I clicked on the new button and it doesn't apply the category.

I must have missed a step somewhere, can you help?

Thank you

Mel
Reply With Quote
  #4  
Old 06-11-2019, 08:02 PM
gmayor's Avatar
gmayor gmayor is offline create macro to add category to email Windows 10 create macro to add category to email Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,761
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

Create an ordinary module and move the code to it.
You may need to refer to https://www.gmayor.com/create_and_em...gital_cert.htm in some versions of Outlook or the security will inhibit the macro.
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 06-11-2019, 10:43 PM
melfinster melfinster is offline create macro to add category to email Windows 10 create macro to add category to email Office 2016
Novice
create macro to add category to email
 
Join Date: Jun 2019
Posts: 3
melfinster is on a distinguished road
Default

great! I got it to work thank you
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
create macro to add category to email Create a Userform in Outlook to select a macro for an email template. snickerbart Outlook 4 04-17-2015 12:55 PM
Create QuickStep: Save Contact with Category ilcaa72 Outlook 1 05-20-2014 10:31 AM
Macro to export email to text file on send depending on category Joe Patrick Outlook 0 10-19-2012 06:20 PM
How to create a Main category mirrored in Sub category data entry type of solution? Geza59 Excel 0 10-19-2012 05:44 AM


All times are GMT -7. The time now is 10:46 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft