13 July 2017

Scripting replies in Outlook

Given my use of hot keys to expand text, it was time to take-on another task: scripting replies in Outlook. I have previously covered how to do this in Apple Mail, compared to which, the following is mouthy. There’s a good reason for that, which I’ll get-to in a minute. But it does the job.

Depending on the email service used, SenderName generates two types, viz.,

John Doe
Doe, John

The first name-type is common among personal email services. Whereas, enterprise email systems, like Microsoft Exchange, generate comma-separated name-types. Some organisations also append org-structures to first names.1 Here’s how mine looks. As you can see, filtering first name out of this complicates things.


The following script addresses all three above cases successfully:

; Scripting replies in Outlook 2016 with AutoHotkey
; Hit Ctrl + Win + r to create a reply email (template) 
; 2017 ckunte
ol := COMObjActive("Outlook.Application").ActiveExplorer.Selection.Item(1)
From := ol.SenderName
StringGetPos, pos, From, `,
if errorlevel
    StringGetPos, pos, From, %A_Space%
    StringLeft, From, From, pos
    StringTrimLeft, From, From, pos+2
StringUpper From, From, T
FirstName := RegExReplace(From, " .*", "") ; Removes all text trailing first name
SendInput, ^+r ; reply all (For reply to sender, but not all, use ^r)
Sleep, 100
SendInput, Hi %FirstName%, {Enter 2}Thank you for your email.{Enter 2}

Insert this snippet into the autohotkey (.ahk) file as an independent entry (i.e., separate by a line space at both ends), customise the last-but-one line to get your own way of addressing people, recompile using Ahk2Exe, run the thus generated .exe file (works as a systray app.), and then hit Ctrl + Win + r by selecting (or opening) a received email in Outlook, and you should see a reply-all window with the following pre-populated:

Hi <FirstName>,

Thank you for your email.

<Signature appears here, if set-up in Outlook>

The actual first name of the sender (to whose email you’d be replying to) will appear in lieu of <FirstName> in the example above.

Scripting replies in Outlook

For someone replying to tens of email messages a day, this is good automation — lots of keystrokes saved. My wrists and fingers aren’t complaining.

  1. Don’t get me started on how archaic this is; I’m certainly not judging. Orgs do what they do; like conveniently adopting eight-hour day from factory work.