Hello, I wanted to share a script that gives me something I've been wanting for a very long time. I wanted to ability to hit any key, say the 'a' key and have my message in Outlook be moved to an archive folder. A combination of an Outlook macro (which does the actual moving of emails) and an AHK script (which calls this macro with a single keystroke) does just that.
I just also add that I tested this with Outlook 2007 and Windows Vista, though it may well work out of the box in other recent versions of Outlook.
I'll start with the Outlook macro that does the actual email handling. I got the idea from:
http://techdem.blogspot.com/2008/03/out ... utton.html
Before proceeding further, let me illustrate how my inbox and archives folders are organized. This can of course be adapted to your own needs, but I thought it helpful to see what I've done to understand the code.
My inbox is organized like so:
Inbox
- Business
- Other folders
Rules move emails into a subfolder if it comes through certain accounts or matches certain criteria.
My archiving scheme is setup like so.
Mail
- Business Received
- Business Sent
- Personal Received
- Personal Sent
So what I wanted was a way for the archiving function to send messages in
Inbox/Business to
Mail/Business Received, but send all other messages to
Mail/Personal Received.
Here's what the outlook macro code looks like. Again, see the above link for how to add macros to Outlook.
Code:
Sub ArchiveSelected()
'set current folder
Set currentFolder = Application.ActiveExplorer.currentFolder
'set inbox
Set inbox = Application.GetNamespace("MAPI").GetDefaultFolder(6)
'Exit macro if the currentFolder has a default item type of anything other than 0 (mail)
'This little hack prevents error if macro is run from the To-Do List. (For some reason, currentFolder.Parent in the To-Do List was causing errors.)
If currentFolder.DefaultItemType <> 0 Then
Exit Sub
End If
'Exit macro if no messages are selected OR we're not in the inbox or a child of the inbox
If (Application.ActiveExplorer.Selection.Count = 0) Or (currentFolder <> inbox And currentFolder.Parent <> inbox) Then
Exit Sub
End If
'set folders to watch
Set Business = Application.GetNamespace("MAPI").Folders("Personal Folders").Folders("Inbox").Folders("Business")
'set folders to archive to
Set Business_Received = Application.GetNamespace("MAPI").Folders("Personal Folders").Folders("Mail").Folders("Business Received")
Set Personal_Received = Application.GetNamespace("MAPI").Folders("Personal Folders").Folders("Mail").Folders("Personal Received")
'Set Archive Folder depending upon the current subfolder of the inbox
'If current subfolder is Business
If currentFolder = Business Then
Set archiveFolder = Business_Received
'if current subfolder is another subfolder you want to watch
'ElseIf currentFolder = [some other folder] Then
' Set archiveFolder = [some other archive folder]
'Otherwise set to a default archive (Personal Received). Works for the inbox and all other folders not referenced above.
Else
Set archiveFolder = Personal_Received
End If
For Each Msg In ActiveExplorer.Selection
'I could have cleared the msg flag, but I like to leave them.
'Msg.ClearTaskFlag
Msg.UnRead = False
Msg.Move archiveFolder
Next Msg
End Sub
Keep in mind that for the purposes of getting this to work with AutoHotKey, I had to create a button on the toolbar and assign a keyboard shortcut to call the macro. This is explained in the link above where I mentioned getting the idea.
I chose alt + period (.) for this shortcut, so now all I needed was a way to have AHK send these keys to outlook when I pressed the 'a' key. But of course I also needed to do this selectively, as I anticipated needing to actually use my 'a' key when typing emails.

Here's the AHK code to call this macro.
Important! - I put a space in front of RichEdit20WPT2 in the ctrlList variable so that this code would wrap on these forums. You'll need to remove it if copying and pasting.
Code:
SendMode Input ; superior speed and reliability.
SetTitleMatchMode 2 ;allow partial match to window titles
;********************
;Hotkeys for Outlook
;********************
;As best I can tell, the window text 'NUIDocumentWindow' is not present
;on any other items except the main window. Also, I look for the phrase
; ' - Microsoft Outlook' in the title, which will not appear in the title (unless
;a user types this string into the subject of a message or task).
#IfWinActive - Microsoft Outlook ahk_class rctrl_renwnd32, NUIDocumentWindow
a::HandleOutlookKeys("!.", "a") ;calls archive macro
f::HandleOutlookKeys("!w", "f") ;forwards message
r::HandleOutlookKeys("!r", "r") ;replies to message
u::HandleOutlookKeys("^u", "u") ;marks messages as unread
y::HandleOutlookKeys("^q", "y") ;marks messages as read
#IfWinActive
;Passes Outlook a special key combination for custom keystrokes or normal key value, depending on context
HandleOutlookKeys( specialKey, normalKey ) {
;Activates key only on main outlook window, not messages, tasks, contacts, etc.
IfWinActive, - Microsoft Outlook ahk_class rctrl_renwnd32, NUIDocumentWindow, ,
{
;Find out which control in Outlook has focus
ControlGetFocus, currentCtrl
;MsgBox, Control with focus = %currentCtrl%
;set list of controls that should respond to specialKey. Controls are the list of emails and the main (and minor) controls of the reading pane, including controls when viewing certain attachments.
;Currently I handle archiving when viewing attachments of Word, Excel, Powerpoint, Text, jpgs, pdfs
;The control 'RichEdit20WPT1' (email subject line) is used extensively for inline editing. Thus it had to be removed. If an email's subject has focus, it won't archive...
ctrlList = Acrobat Preview Window1,AfxWndW5,AfxWndW6,EXCEL71,MsoCommandBar1,OlkPicturePreviewer1,paneClassDC1, RichEdit20WPT2,RichEdit20WPT4,RichEdit20WPT5,RICHEDIT50W1,SUPERGRID1,_WwG1
if currentCtrl in %ctrlList%
{
Send %specialKey%
;Allow typing normalKey somewhere else in the main Outlook window. (Like the search field or the folder pane.)
} else {
Send %normalKey%
}
;Allow typing normalKey in another window type within Outlook, like a mail message, task, appointment, etc.
} else {
Send %normalKey%
}
}
Notice that I have a few other short cuts in here as well. Pressing 'r' will reply to an email. Pressing 'f' will forward it. 'u' marks a message as unread. 'ctrl+u' marks it as read.
Finally, Outlook users also might want to see
this AHK script I wrote that improves sending a new message in Outlook.