-
Notifications
You must be signed in to change notification settings - Fork 71
/
SaveAs.ahk
64 lines (58 loc) · 2.36 KB
/
SaveAs.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
; This script saves the active mail item to a specific folder.
; The two hotkeys are the same except they use different folders.
#IfWinActive ahk_exe OUTLOOK.EXE
#1:: ; Win+1 hotkey - Save As C:\FolderA
OutLookSaveAs2("C:\FolderA") ; Call the OutLookSaveAs2 function
return
#2:: ; Win+2 hotkey - Save As C:\FolderB
OutLookSaveAs2("C:\FolderB") ; Call the OutLookSaveAs2 function
return
; Saves the selected Mail Item as:
; <FolderPath>\<Subject>(n).msg
; 'n' is a number that is appended if the file already exists.
OutLookSaveAs2(FolderPath)
{
; Constants
static olExplorer := 34
static olInspector := 35
static olMail := 43
static olMSG := 3
olApp := ComObjCreate("Outlook.Application") ; Gets or creates an instance of Outlook.
objWindow := olApp.ActiveWindow ; Get the active window so we can determine if an Explorer or Inspector window is active.
if (objWindow.Class = olExplorer) ; 'Explorer' -- The main Outlook window.
{
MySelection := objWindow.Selection
if (MySelection.Count > 0)
objMailItem := MySelection.Item(1) ; Gets only the first item if multiple items are selected.
}
else if (objWindow.Class = olInspector) ; 'Inspector' -- The email is open in its own window.
objMailItem := objWindow.CurrentItem
if (objMailItem.Class != olMail) ; If objMailItem is not an email.
return
FileName := objMailItem.Subject ; Gets the subject from the mail item
FileName := RemoveIllegalChars(FileName) ; Remove characters that are not allowed in file names
FilePath := FindFreeName(FolderPath, FileName, "msg") ; Find a file that doesn't already exist
objMailItem.SaveAs(FilePath, olMSG) ; Save the message.
}
; Find an unused file name.
FindFreeName(FileDir, FileName, FileExt)
{
FilePath := FileDir "\" FileName "." FileExt
if FileExist(FilePath) ; If the file exists, loop until an unused name is found.
{
Loop,
{
FilePath := FileDir "\" FileName "(" (A_Index + 1) ")." FileExt
if !FileExist(FilePath)
break
}
}
return, FilePath
}
; Removes characters that are not allowed in file names (\ / : * ? " < > |)
RemoveIllegalChars(FileName)
{
for i, Char in ["\", "/", ":", "*", "?", """", "<", ">", "|"]
FileName := StrReplace(FileName, Char)
return FileName
}