Page 1 of 1

Outlook email SaveAs

Posted: 31 Jan 2018, 13:54
by sttrebo
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

Re: Outlook email SaveAs

Posted: 31 Jan 2018, 16:52
by FanaticGuru
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

Re: Outlook email SaveAs

Posted: 31 Jan 2018, 17:08
by sttrebo
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?

Re: Outlook email SaveAs

Posted: 31 Jan 2018, 19:17
by sttrebo
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.

Re: Outlook email SaveAs

Posted: 31 Jan 2018, 19:24
by FanaticGuru
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