farrissf

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
I have been wanting to use Categories to identify my emails for my construction projects, I ran across this article Create a List of Color Categories to Merge or Restore which gave me the key to uploading all my project codes as categories. Thanks to you, I am all set up to start using my new codes. My plan was to put the emails in folders so I could find them. I saw a really cool tool on a website you had a link to called, Category Manager on the VB Office.net site. I was fine with paying for it so I tried to download but I have no admin rights, I am not interested in losing my job and I have tons of macros and add-ins in Excel so I decided to try and create something. This has turned into a nightmare but I am knee-deep and would like to finish it. I have used my own basic knowledge with ChatGPT to get this far, but I am stuck. I started with this and it works but as you will see it is not user-friendly.

Code:
Sub CategorizeEmailsWithInputBox()
    Dim olApp As Outlook.Application
    Dim olNamespace As Outlook.NameSpace
    Dim olFolder As Outlook.MAPIFolder
    Dim olMail As Object
    Dim keywords As String
    Dim keywordArray() As String
    Dim keyword As Variant
    Dim selectedCategory As String

    ' Prompt for keyword input
    keywords = InputBox("Enter keywords to search for (separate with commas):", "Keyword Input")

    ' Check if any keywords were provided
    If keywords = "" Then
        Exit Sub ' Exit if no keywords entered
    End If

    ' Split the keywords into an array
    keywordArray = Split(keywords, ",")

    ' Initialize Outlook objects
    Set olApp = Outlook.Application
    Set olNamespace = olApp.GetNamespace("MAPI")

    ' Specify the folder where you want to search for emails
    Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox)

    ' Prompt for category selection using InputBox
    selectedCategory = InputBox("Enter the category for the selected emails:", "Category Selection")

    ' Check if a category was selected
    If selectedCategory = "" Then
        Exit Sub ' Exit if no category selected
    End If

    ' Loop through all emails in the folder
    For Each olMail In olFolder.Items
        For Each keyword In keywordArray
            If InStr(1, olMail.Subject, Trim(keyword), vbTextCompare) > 0 Then
                ' Assign the selected category to the email
                olMail.categories = selectedCategory
                olMail.Save
                Exit For ' Exit loop if a match is found
            End If
        Next keyword
    Next olMail

    ' Clean up objects
    Set olMail = Nothing
    Set olFolder = Nothing
    Set olNamespace = Nothing
    Set olApp = Nothing
End Sub

I want to modify the code to where it would give me a dropdown list of the categories. I tried to cajole ChatGPT into creating something but it has failed at every turn.

User Form Code:
Code:
Private Sub CommandButton1_Click()
LstNo = ComboBox1.ListIndex
Unload Me

End Sub


Private Sub UserForm_Initialize()

    Dim objNameSpace As NameSpace
    Dim objCategory As Category
    Dim strOutput As String

    Set objNameSpace = Application.GetNamespace("MAPI")
        If objNameSpace.categories.Count > 0 Then
            For Each objCategory In objNameSpace.categories
                strOutput = objCategory.Name
        With Me.ChosenCategory
        .AddItem (strOutput)
        End With
            Next
        With Me.ChosenCategory
        .AddItem ("Clear Category")
        End With

        End If

End Sub

2023-10-29_13-50-35.png


Module

Code:
Sub CategorizeEmailsWithUserForm()
    Dim olApp As Outlook.Application
    Dim olNamespace As Outlook.NameSpace
    Dim olFolder As Outlook.MAPIFolder
    Dim olMail As Object
    Dim keywords As String
    Dim keywordArray() As String
    Dim keyword As Variant
    Dim selectedCategory As String
    
    ' Prompt for keyword input
    keywords = InputBox("Enter keywords to search for (separate with commas):", "Keyword Input")
    
    ' Check if any keywords were provided
    If keywords = "" Then
        Exit Sub ' Exit if no keywords entered
    End If
    
    ' Split the keywords into an array
    keywordArray = Split(keywords, ",")
    
    ' Initialize Outlook objects
    Set olApp = Outlook.Application
    Set olNamespace = olApp.GetNamespace("MAPI")
    
    ' Specify the folder where you want to search for emails
    Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox)
    
    ' Show the CategorySelectionForm UserForm
    UserForm1.Show
    
    ' Retrieve the selected category from the ComboBox on the UserForm
    selectedCategory = CategorySelectionForm.ComboBox1.Value
    
    ' Check if a category was selected
    If selectedCategory = "" Then
        Exit Sub ' Exit if no category selected
    End If
    
    ' Loop through all emails in the folder
    For Each olMail In olFolder.Items
        For Each keyword In keywordArray
            If InStr(1, olMail.Subject, Trim(keyword), vbTextCompare) > 0 Then
                ' Assign the selected category to the email
                olMail.categories = selectedCategory
                olMail.Save
                Exit For ' Exit loop if a match is found
            End If
        Next keyword
    Next olMail
    
    ' Clean up objects
    Set olMail = Nothing
    Set olFolder = Nothing
    Set olNamespace = Nothing
    Set olApp = Nothing
End Sub

I would prefer to have a well-thought-out approach (an app like the Catagory Manager) I am not opposed to paying for it within reason (hopefully less than a hundy). The macro I had in mind would comb through my email looking for keywords and then apply a category.

I have really enjoyed your approach to macros, even if this post does not result in a solution, I wanted to say thanks for what you do. I would never even dreamed of these categories as a solution without your website, thank you!

farrissf
 
I'm surprised chatgpt gave you usable code. When I ask it for Outlook macros it gives me bad code, often with methods that don't exist in Outlook object model. :(

Do you want a dropdown list or will the category dialog do?

.ShowCategoriesDialog

VBOffice has code samples
Outlook VBA Macros - VBOffice

(I'm out of the office this week and on my macbook so can't test your code right now. )
 
I'm surprised chatgpt gave you usable code. When I ask it for Outlook macros it gives me bad code, often with methods that don't exist in Outlook object model. :(

Do you want a dropdown list or will the category dialog do?

.ShowCategoriesDialog

VBOffice has code samples
Outlook VBA Macros - VBOffice

(I'm out of the office this week and on my macbook so can't test your code right now. )
Category dialog will work perfectly.

Thank you for responding!
 
Category dialog will work perfectly.

Thank you for responding!
Category dialog will work perfectly.

Thank you for responding!

I'm surprised chatgpt gave you usable code. When I ask it for Outlook macros it gives me bad code, often with methods that don't exist in Outlook object model. :(

Do you want a dropdown list or will the category dialog do?

.ShowCategoriesDialog

VBOffice has code samples
Outlook VBA Macros - VBOffice

(I'm out of the office this week and on my macbook so can't test your code right now. )
I have had pretty good luck with ChatGPT and Excel VBA but you have to do a lot of testing. I found that you are much better off if you ask it about small to medium size pieces of your macro at a time. If you knew nothing about VBA it would be useless. Kinda like it is with Outlook. Has anyone had any luck using AutoMacro with Outlook VBA. I am stuck at this point.
 
Similar threads
Thread starter Title Forum Replies Date
J Outlook Rules not show Master Category List Using Outlook.com accounts in Outlook 4
M Searching for categories not in the master category list Using Outlook 3
L Category List Using Outlook 43
R Restore Master Category List Outlook 2013 Using Outlook 7
K Master Category List / Contacts Public Folder Advice Using Outlook 4
L Anyway to Display entire category list in Outlook 2007 automatically? Using Outlook 1
B Outlook 2019 Automatically move email after assigning category Using Outlook 4
F Add a category before "Send an Email When You Add an Appointment to Your Calendar" Outlook VBA and Custom Forms 0
V Macro to mark email with a Category Outlook VBA and Custom Forms 4
P Emails assigned with a certain category (within a shared inbox) to be copied to a specific folder. Outlook VBA and Custom Forms 2
R Clear one category when another is chosen Outlook VBA and Custom Forms 1
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
R Auto Assign Category colours to Incoming Emails based on whom the email is addressed Outlook VBA and Custom Forms 3
A Unflag Inbox and Flag Inbox with Orange Category After Item is send Outlook VBA and Custom Forms 3
bhamberg Contacts in a category Outlook VBA and Custom Forms 11
K Custom Category Colors Using Outlook 2
4 Macro to set the category of Deleted Item? Outlook VBA and Custom Forms 2
K Update Appointment category when changed in Excel Using Outlook 3
soadfan assign category (VBA) Using Outlook 7
J Automatic color category according to variable domains Outlook VBA and Custom Forms 4
Diane Poremsky Category Color doesn't Display in Inbox Using Outlook 0
Wolfkc Unwanted Category "Starred in Android" Using Outlook 6
M receive mail when appointment category changes and create task from appointment Outlook VBA and Custom Forms 0
N VBA Outlook reference to Excel and assigning category Outlook VBA and Custom Forms 13
A recover contact category assignments Using Outlook 4
A Grouping emails using "From" causes a None Category Using Outlook 5
William getting custom form to load category colors Outlook VBA and Custom Forms 4
Diane Poremsky Categorize Messages using Contact Category Using Outlook 0
T Macro to find contacts by category and copy them to another folder Outlook VBA and Custom Forms 15
bifjamod Saving sent email to specific folder based on category with wildcard Outlook VBA and Custom Forms 1
K Outlook 2010 - Set the category based on category of other emails in same conversatio Using Outlook 20
A How are you handling Outlook.com category issue and syncing Using Outlook.com accounts in Outlook 2
T default category Outlook VBA and Custom Forms 2
R click to toggle quick click category reasigns to where ? Using Outlook 2
L Change Category in a Task via macro Using Outlook 8
K Help! I selected every contact and mistakenly made them all the same category Using Outlook 1
R Filter Views by Category Using Outlook 2
S categories are being cut off after selecting from category pop up form Using Outlook 3
D Search on Category in Outlook 2013 Using Outlook 1
R Any idea why my category colours aren't showing in my calendar? Using Outlook 0
F Export Outlook contacts by One Category Using Outlook 1
M Cannot change category in inbox; Can change it in popup Using Outlook 0
D Macro to export mail data based on assigned category Using Outlook 3
C Custom form with custom colour category visible to all users in public folders Using Outlook 1
J Contacts: Assigning followup date by category Using Outlook 2
C Apply Category Color for Appointment Background Color Using Outlook 2
J Calendar Search by Category not showing expected results Using Outlook 7
S Printing Category Key on Calendar Using Outlook 1
A Can Rule Check Category Contact is assigned? Using Outlook 1
J View All Category Items Using Outlook 1

Similar threads

Back
Top