Automatic email categorize in Outlook

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jeronimo
Posts: 7
Joined: 16 Nov 2022, 05:59

Automatic email categorize in Outlook

Post by Jeronimo » 16 Nov 2022, 06:07

Hi guys, I'm using script for automatically marking and closing emails in outlook but it won't close emails [as read]

Please help me to understand what do i do wrong?
Example.ahk
(180 Bytes) Downloaded 21 times

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Automatic email categorize in Outlook

Post by boiler » 16 Nov 2022, 06:13

It would be better if you posted your code directly in the body of your message (between [code][/code] tags) so we don’t have to download your file to view your simple script.

Jeronimo
Posts: 7
Joined: 16 Nov 2022, 05:59

Re: Automatic email categorize in Outlook

Post by Jeronimo » 16 Nov 2022, 06:21

Code: Select all


{

Send, c

Sleep, 1

Send, Client 1

Sleep, 1

Send {Enter} 

}

Return

Jeronimo
Posts: 7
Joined: 16 Nov 2022, 05:59

Re: Automatic email categorize in Outlook

Post by Jeronimo » 17 Nov 2022, 09:10

No one who can help with this one?

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Automatic email categorize in Outlook

Post by boiler » 17 Nov 2022, 09:27

It would be much more reliable to interact with Outlook via the COM interface rather than sending key presses to the window. This is a good place to get started. There are a lot of examples on the forum.

User avatar
Datapoint
Posts: 294
Joined: 18 Mar 2018, 17:06

Re: Automatic email categorize in Outlook

Post by Datapoint » 17 Nov 2022, 15:22

Maybe something like this.

Esc closes the script
F6 displays the current categories
F7 removes all categories
F8 sets categories to "Yellow Category"

Code: Select all

Esc::ExitApp ; Exit this script

; Microsoft Outlook hotkeys (WinActive)
#IfWinActive ahk_class rctrl_renwnd32

F6::MsgBox % OutlookCategorize() ; Display current categories
F7::OutlookCategorize("") ; Clear categories (set categories to blank)
F8::OutlookCategorize("Yellow Category") ; Set categories


; OutlookCategorize
; Returns or sets a string representing the categories of the selected Outlook item.
;   NewCategories       If 0 (zero), returns a string representing the current categories of the Outlook MailItem
;                       Or a string representing the categories that will be assigned to the Outlook MailItem
OutlookCategorize(NewCategories := 0)
{
    static olExplorer := 34
    static olInspector := 35
    static olSave := 0

    ; Get the selected email
    myWindow := ComObjActive("Outlook.Application").ActiveWindow
    if (myWindow.Class = olExplorer) ; The main Outlook window
        MailItem := myWindow.Selection(1) ; The first MailItem that is selected
    else if (myWindow.Class = olInspector) ; The email is open in its own window
        MailItem := myWindow.CurrentItem
    ; Return current categories or set new categories
    if (NewCategories = 0)
        return MailItem.Categories
    else
    {
        MailItem.Categories := NewCategories
        MailItem.Close(olSave)
    }
}

Jeronimo
Posts: 7
Joined: 16 Nov 2022, 05:59

Re: Automatic email categorize in Outlook

Post by Jeronimo » 18 Nov 2022, 08:47

Thank you for your option, would this script mark all incoming emails or only one by one?

User avatar
Datapoint
Posts: 294
Joined: 18 Mar 2018, 17:06

Re: Automatic email categorize in Outlook

Post by Datapoint » 18 Nov 2022, 11:57

Yes it's one by one. The above script will be triggered by a hotkey, and the email either needs to be open in its own window or selected in the main window.

You might be able to use rules instead of a script. You can setup Outlook to assign emails to categories based on rules, mark them as read, move them to folders, etc. See the part on the following page titled Apply Categories Automatically with Rules Using the Outlook Desktop App

It should also be possible to find all unread messages and then loop through them. It depends on what you are trying to accomplish with the script, but I would suggest trying the rules out first. If that doesn't work maybe give some more explanation about what is supposed to happen, we might be able to do something with a script but that depends on what you want to do.

This example is a script that responds when a new message is sent to the inbox. It changes the category of the email and marks it as read.
The NewMailEx method will be run every time the event is fired in Outlook.
Ctrl+Esc hotkey exits the script.

Code: Select all

olApp := ComObjActive("Outlook.Application")
ComObjConnect(olApp, new Outlook_Events)
return

class Outlook_Events
{
    static olFolderInbox := 6
    
    __Call(Event, Args*)
    {
        ; Uncomment to show all Outlook application events
        ;~ EventInfo := ""
        ;~ for key, val in Args
            ;~ EventInfo .= "Arg#: " Key "`nVarType: " Format("{:X}", ComObjType(val)) "`nIName: "ComObjType(val, "Name")
                ;~ . "`nIID: " ComObjType(val, "IID") "`nCName: " ComObjType(val, "Class") "`nCLSID: " ComObjType(val, "CLSID") "`n`n"
        ;~ MsgBox,, % "Application." Event " - " Args[Args.MaxIndex()].Name, % EventInfo
    }
    NewMailEx(EntryIDCollection, olApp)
    {
        ;~ MsgBox % EntryIDCollection
        ; https://learn.microsoft.com/en-us/office/vba/outlook/How-to/Items-Folders-and-Stores/working-with-entryids-and-storeids
        olns := olApp.Session ; Namespace
        olFolder := olns.GetDefaultFolder(Outlook_Events.olFolderInbox) ; Get the Inbox folder
        ;~ MsgBox % olFolder.Name "`n" Outlook_Events.olFolderInbox
        StoreID := olFolder.StoreID ; Get the StoreID, which is a property of the folder. 
        MailItem := olns.GetItemFromID(EntryIDCollection, StoreID) ; Get the MailItem object from the entry ID
        MailItem.Categories := "Yellow Category" ; Assign category to the mail item
        MailItem.Unread := 0 ; Mark as read
        MailItem.Save
    }
}

^Esc::
ExitApp

Jeronimo
Posts: 7
Joined: 16 Nov 2022, 05:59

Re: Automatic email categorize in Outlook

Post by Jeronimo » 22 Nov 2022, 04:43

Why key would start the scirpt? i cannot start it manualy

Jeronimo
Posts: 7
Joined: 16 Nov 2022, 05:59

Re: Automatic email categorize in Outlook

Post by Jeronimo » 22 Nov 2022, 04:55

Sorry for going back and forth i just need a scprit that will put specific categorize section on each emial when i press the button on my keyboard

User avatar
Datapoint
Posts: 294
Joined: 18 Mar 2018, 17:06

Re: Automatic email categorize in Outlook

Post by Datapoint » 26 Nov 2022, 20:29

Jeronimo wrote:
22 Nov 2022, 04:55
Sorry for going back and forth i just need a scprit that will put specific categorize section on each emial when i press the button on my keyboard
That's what my first post does.

Also, I suggest you explore Outlook Rules as an option too.

Jeronimo
Posts: 7
Joined: 16 Nov 2022, 05:59

Re: Automatic email categorize in Outlook

Post by Jeronimo » 01 Dec 2022, 17:09

It's not working, unfortunately

Is it possible to have something for using categorize only on emails that i want to categorize?

Not having it marking automatically but manualy?

Post Reply

Return to “Ask for Help (v1)”