Outlook email SaveAs

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Outlook email SaveAs

31 Jan 2018, 13:54

I have a small script that I use with my task planning app. this script just writes the subject of the email to a text file. But i've been trying to get the script to also save the selected email outside of outlook. In essence, I want to do a saveas and save the email as a .msg (the link to that file is added to the text file. Can anyone show me how to modify this script to save the email? I've been looking thru the COM forum posts, but I'm not getting it somewhere.

thanks in advance for any help.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

ToDoFile = todo.txt
Priority = (A)
Project = +Inbox

F7::
olApp := ComObjActive("Outlook.Application")
Subject := olApp.ActiveInspector.CurrentItem.Subject
If Subject =
	{
	GoTo, Blank
	}
	else
	{
	PSubject := RegExReplace(Subject,"^(?:RE|FW): ")
	PSubject := StrReplace(PSubject, "+", "_")
	FileRead, TDFile, %ToDoFile%
	FileDelete, %ToDoFile%
	sleep, 100
	FileAppend, %Priority% %PSubject% %Project% file:///%PSubject%.msg`r`n%TDFile%, %ToDoFile%
	}
return

Blank:
Gui, 3:+AlwaysOnTop +ToolWindow -SysMenu -Caption
Gui, 3:Color, ffffff ;changes background color
Gui, 3:Font, cFF0000 s20 wbold, Verdana ;changes font color, size and font
Gui, 3:Add, Text, x0 y0, Open an email for this! ;the text to display
Gui, 3:Show, NoActivate, Xn: 0, Yn: 0

sleep, 1000

Gui, 3:Destroy
return
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook email SaveAs

31 Jan 2018, 16:52

I did not really look at your code but he is a simple example of SaveAs for an email.

Code: Select all

F12::
	olApp := ComObjActive("Outlook.Application")
	try
		olItem := olApp.ActiveWindow.CurrentItem
	catch
		olItem := olApp.ActiveExplorer.Selection.Item(1)
	olItem.SaveAs(A_ScriptDir "\" olItem.Subject ".msg", 3)
return
If you have an email open that is what is used, if not it looks for the first selected email.

This does no checking at all and will probably result in illegal file names if not careful.

You can look up and change the 3 to other options for format of the save file.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Re: Outlook email SaveAs

31 Jan 2018, 17:08

excellent, thanks. just need to add in a filter to remove invalid characters from the subjects and it'll be perfect.
I use this in autohotkey:

PSubject := RegExReplace(Subject,"^(?:RE|FW): ")
PSubject := StrReplace(PSubject, "+", "_")

any way of doing a similar thing here?
also, what is the "try" and "catch" statements for?
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Re: Outlook email SaveAs

31 Jan 2018, 19:17

easier than I thought:
olItem.SaveAs(Storage_Dir "\" RegExReplace(olItem.Subject, "^(?:RE|FW): ") ".msg", 3)

removes the offending characters and any other I choose to add into the RE|FW etc string.

think i'm good to go. thanks for your help.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook email SaveAs

31 Jan 2018, 19:24

sttrebo wrote:PSubject := RegExReplace(Subject,"^(?:RE|FW): ")
PSubject := StrReplace(PSubject, "+", "_")

any way of doing a similar thing here?
Same way. Subject := olItem.Subject will but the subject of the email into a variable named Subject.

Then use whatever to construct the filename and path. Once you get the info in a variable...

Then just do:
olItem.SaveAs(FileNameWithPath, 3)
sttrebo wrote:also, what is the "try" and "catch" statements for?
try just means try the next line but if you get an error than catch it and do something else instead.

First it tries to connect to an open email but if there is not one it will produce an error which gets catch and then it connects to a selected email instead.

You also need to strip out [\\/:"*?<>|]+ which are all invalid for a Windows filename.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, haomingchen1998, mikeyww, Oblomov228, RussF and 262 guests