Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Archiving email in Outlook with a single keystroke


  • Please log in to reply
5 replies to this topic
jayp
  • Members
  • 23 posts
  • Last active: Jan 04 2010 10:51 PM
  • Joined: 07 Mar 2009
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: <!-- m -->http://techdem.blogs... ... utton.html<!-- m -->

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.

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.

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.

Demokos
  • Members
  • 105 posts
  • Last active: Jan 20 2011 06:28 PM
  • Joined: 28 Dec 2005
May be you can do it only with autohotkey with the help of MS Office Automation Functions (via COM) and (of course) of the COM Standard Library, especially since that the new doted notation exists in the COM Standard Library (or look at COM_InvokeDeep - climb a COM tree more easily
You can look at ADO COM - Database Query for an example of the doted notation of COM Standard Library (last page).

jayp
  • Members
  • 23 posts
  • Last active: Jan 04 2010 10:51 PM
  • Joined: 07 Mar 2009
Demokos, that's a good suggestion. However, I've poured a ton of time into creating some AHK scripts in the last few days. (I was sick, at home, and had just really started using AHK.) So I'm not going to try this myself because I've got it working and working well... But if you or anyone else wants to take a stab at it, I guess some people might like that.

On the other hand, one advantage of the method as I've implemented it is that you can run the archive function from inside Outlook by clicking a button. Some people, my wife for example, just don't take to use keyboard shortcuts. For these folks, a GUI button offers the best of both worlds.

And of course I feel a little silly saying this to the audience here at the AHK forums... :D Who wants a button when there's AHK!

RocknRoller
  • Guests
  • Last active:
  • Joined: --

And of course I feel a little silly saying this to the audience here at the AHK forums... Very Happy Who wants a button when there's AHK!


I do! Thanks for the Outlook Macro! :)

jayp
  • Members
  • 23 posts
  • Last active: Jan 04 2010 10:51 PM
  • Joined: 07 Mar 2009
I fixed a bug... It turns out that when you perform an inline edit (as opposed to opening the item for editing) on tasks, calendar, contacts, etc, the class name is RichEdit20WPT1. Well, this is also the class name for an email's subject line. So my AHK script was preventing typing normal keys when performing an inline edit.

By removing RichEdit20WPT1 I've fixed the problem, with a minor loss in functionality... If in the reading pane you give the subject line of an email focus, then these hotkeys won't work...

jayp
  • Members
  • 23 posts
  • Last active: Jan 04 2010 10:51 PM
  • Joined: 07 Mar 2009
Oops. I neglected to include in my code the setting for SetTitleMatchMode to allow for partial title matches. I've updated the code in the original post to reflect this setting. Sorry.