Outlook COM: add attachment to open mail Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Outlook COM: add attachment to open mail

10 May 2021, 01:49

I am trying to add attachments to the active new outlook email with a menu to chose from the last 10 created files in a specific folder.

This currently works perfect when you have the new mail open in a separate message window, but fails when the new mail is embedded in the main Outlook window (you could descripe this as "not popped out").
So the line oItem := ComObjActive("Outlook.Application").ActiveExplorer.Selection.Item(1) fails to connect to the active mailItem.

Code: Select all

F12::
{
	; Create shortcutmenu for recent documents.
	File_List :=""
	loop, Files, E:\users\DGE1507\PLOT\*
	{
		currentTime := A_now
		EnvSub, currentTime, %A_LoopFileTimeModified%, Hours
		File_List .= currentTime "`t" A_Loopfilefullpath "`t" A_LoopFileName "`n"
		
	}
	Sort, File_List, N
	Menu, mFilesNew, Add, 
	Menu, mFilesNew, DeleteAll
	loop, Parse, File_list, `n
	{
		; only show the 10 last items
		If (A_index=11){
			break
		}
		if (A_Loopfield=""){
			continue
		}
		arrFile := StrSplit(A_Loopfield, "`t")
		BoundGivePar := Func("Outlook_AddAttachments").Bind(arrFile.2)
		Menu, mFilesNew, Add, % arrFile.3, % BoundGivePar
		
	}
	
	Menu, mFilesNew, Show
	return
}

Outlook_AddAttachments(File){
	WinGetTitle, WinTitle, A
	if InStr(WinTitle, "- Message"){
		oItem := ComObjActive("Outlook.Application").ActiveWindow.CurrentItem	; connects to open message if separate window
	}
	Else{
		; Connect to outlook mail but this does not work...
                oItem := ComObjActive("Outlook.Application").ActiveExplorer.Selection.Item(1)			
	}
	Subject := oItem.subject
	oItem.Attachments.Add(File)
return
} 
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Outlook COM: add attachment to open mail

14 Jul 2021, 05:30

I tried some things and came up with the following code, but when the reply mail is inbedded in the outlook window, I need to send an Control+s to save it first in the drafts folder. There is also a possibility for errors because it just checks if the subject of the latest draft is similar.

So the following code will probably work quite well, but is certainly not 100% errorproof :think: .
If somebody has a better way, feel free to post an improvement.

Code: Select all

Outlook_AddAttachments(File){
	WinGetTitle, WinTitle, A
	if InStr(WinTitle, "- Message"){
		oItem := ComObjActive("Outlook.Application").ActiveWindow.CurrentItem	; connects to open message if separate window
	}
	Else{
		
		WinActivate,  Ahk_exe outlook.exe
		SendInput, ^s
		
		oOutlook := ComObjActive("Outlook.Application")
				
		SubjectSelected:= oOutlook.ActiveExplorer.Selection.Item(1).Subject
		
		olFolderDrafts := 16
		DraftsCount := oOutlook.Session.GetDefaultFolder(olFolderDrafts).Items.Count
		oItem := oOutlook.Session.GetDefaultFolder(olFolderDrafts).Items[DraftsCount]
		SubjectLastDraft:= oMail.Subject
		SubjectLastDraft:= StrReplace(SubjectLastDraft, "RE: ")
		SubjectLastDraft:= StrReplace(SubjectLastDraft, "FW: ")
		if InStr(SubjectSelected, SubjectLastDraft){
			DebugWindow("draft found to target: " oMail.Subject "`n")
		}
		else{
			return
		}
		
	}
	Subject := oItem.subject
	DebugWindow("Subject: " Subject "`n")
	oItem.Attachments.Add(File)
	return
}
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Outlook COM: add attachment to open mail

14 Jul 2021, 11:20

I'm not sure that I'll be able to help you
So just some thoughts:
- I'd say WinActivate, Ahk_exe outlook.exe makes more sense as the first line of the function, then you get the active title.
- The variable oMail appears twice but is not actually defined, maybe you changed that name for oItem at some point?
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook COM: add attachment to open mail

14 Jul 2021, 17:49

AHK_user wrote:
10 May 2021, 01:49
I am trying to add attachments to the active new outlook email with a menu to chose from the last 10 created files in a specific folder.

This currently works perfect when you have the new mail open in a separate message window, but fails when the new mail is embedded in the main Outlook window (you could descripe this as "not popped out").
So the line oItem := ComObjActive("Outlook.Application").ActiveExplorer.Selection.Item(1) fails to connect to the active mailItem.

Code: Select all

F12::
{
	; Create shortcutmenu for recent documents.
	File_List :=""
	loop, Files, E:\users\DGE1507\PLOT\*
	{
		currentTime := A_now
		EnvSub, currentTime, %A_LoopFileTimeModified%, Hours
		File_List .= currentTime "`t" A_Loopfilefullpath "`t" A_LoopFileName "`n"
		
	}
	Sort, File_List, N
	Menu, mFilesNew, Add, 
	Menu, mFilesNew, DeleteAll
	loop, Parse, File_list, `n
	{
		; only show the 10 last items
		If (A_index=11){
			break
		}
		if (A_Loopfield=""){
			continue
		}
		arrFile := StrSplit(A_Loopfield, "`t")
		BoundGivePar := Func("Outlook_AddAttachments").Bind(arrFile.2)
		Menu, mFilesNew, Add, % arrFile.3, % BoundGivePar
		
	}
	
	Menu, mFilesNew, Show
	return
}

Outlook_AddAttachments(File){
	WinGetTitle, WinTitle, A
	if InStr(WinTitle, "- Message"){
		oItem := ComObjActive("Outlook.Application").ActiveWindow.CurrentItem	; connects to open message if separate window
	}
	Else{
		; Connect to outlook mail but this does not work...
                oItem := ComObjActive("Outlook.Application").ActiveExplorer.Selection.Item(1)			
	}
	Subject := oItem.subject
	oItem.Attachments.Add(File)
return
} 

The problem is getting the correct link to the reply when it is inline. Maybe this will help:

Code: Select all

#F11::
	olApp := ComObjActive("Outlook.Application")
	olMailItem := olApp.ActiveExplorer.Selection.Item(1)
	MsgBox % olMailItem.Subject
	olReply := olApp.ActiveExplorer.ActiveInlineResponse
	MsgBox % olReply.Subject
return

Esc::ExitApp

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
User avatar
Datapoint
Posts: 295
Joined: 18 Mar 2018, 17:06

Re: Outlook COM: add attachment to open mail  Topic is solved

14 Jul 2021, 18:04

This is the same answer as FanaticGuru, but I added a part to check for an Explorer/Inspector window so you don't have to use if InStr(WinTitle, "- Message").

Code: Select all

Outlook_AddAttachments("C:\Test\New Text Document.txt")
return

Outlook_AddAttachments(File)
{
    olApp := ComObjActive("Outlook.Application")
    if (olApp.ActiveWindow.Class = 34)
        olItem := olApp.ActiveExplorer.ActiveInlineResponse
    else if (olApp.ActiveWindow.Class = 35)
        olItem := olApp.ActiveWindow.CurrentItem
    olItem.Attachments.Add(File)
}
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Outlook COM: add attachment to open mail

15 Jul 2021, 05:38

Thanks @Datapoint and @FanaticGuru , It works perfect! :bravo:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Lamron750, Rohwedder and 229 guests