Jump to content


Photo

List Attachments in Body of Outlook Email


  • Please log in to reply
5 replies to this topic

#1 Fanatic Guru

Fanatic Guru
  • Members
  • 48 posts

Posted 17 July 2012 - 09:14 PM

I would like a script so that when I am composing an email in Outlook 2010 under Windows 7 so that I can hit a hotkey and it will inserts a list of all the file names of the attachments I currently have attached to the email I am composing into the body of the email at my current cursor position.

I have tried to figure it out using COM and some VBA scripts but I just can not get it figured out.

The below link contains some interesting information from a VBA script:
<!-- m -->http://www.gregthatc... ... ments.aspx<!-- m -->

It will gather lots of information about a selected email's attachments and report them in a new email. But I need it to use the current email that is being composed for the attachments and put it out at my current cursor position. Also I would much brother it be a AHK.

Several post by sinkfaze in the forums provide some insight also but alas the coding is just beyond my ability.

I imagine it is possible and probably not even that long of a script if you really know your COM commands and syntax.

Any help that can be provided would greatly be appreciated.

Thanks,
FG

#2 jethrow

jethrow
  • Fellows
  • 2549 posts

Posted 19 July 2012 - 06:53 AM

F1::

clip := ClipboardAll

Clipboard := ""

t := ""

MailItem := ComObjActive("Outlook.Application").ActiveWindow.CurrentItem

Attachments := MailItem.Attachments

Loop % Attachments.Count

   t .= Attachments.item[A_Index].FileName "`n"

Clipboard := SubStr(t,1,-1)

ClipWait

Sleep 10

Send ^v

Clipboard := clip

return


#3 sinkfaze

sinkfaze
  • Moderators
  • 6089 posts

Posted 19 July 2012 - 12:11 PM

Keep in mind that what you and I call attachments and what Outlook calls attachments are different things. If you have images embedded in the body of an email, those are also considered attachments by Outlook even though they do not appear in the attachments section of the email as one typically expects.

#4 Fanatic Guru

Fanatic Guru
  • Members
  • 48 posts

Posted 19 July 2012 - 09:31 PM

F1::
clip := ClipboardAll
Clipboard := ""
t := ""
MailItem := ComObjActive("Outlook.Application").ActiveWindow.CurrentItem
Attachments := MailItem.Attachments
Loop % Attachments.Count
   t .= Attachments.item[A_Index].FileName "`n"
Clipboard := SubStr(t,1,-1)
ClipWait
Sleep 10
Send ^v
Clipboard := clip
return


Thanks very much.

I have been tinkering with this for several days trying to learn some of the ins and outs of COM.

I was all around it, sometimes close, sometimes completely off mark as I tried different things but never could quite get it.

My closest attempt I was doing something like this:

t := Attachments.CurrentItem[A_Index].FileName "`n"

instead of this:

t .= Attachments.item[A_Index].FileName "`n"

And I was not doing it through the Clipboard but I don't know if that matters.

One small point though. Your script did not work for me initially but after some tinkering I discovered it was a timing issue and added another Sleep 10. It now appears to be working very well.

Copy Paste of code that works for me.
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


#a::
clip := ClipboardAll
Clipboard := ""
t := ""
MailItem := ComObjActive("Outlook.Application").ActiveWindow.CurrentItem
Attachments := MailItem.Attachments
Loop % Attachments.Count
   t .= Attachments.item[A_Index].FileName "`n"
Clipboard := SubStr(t,1,-1)
ClipWait
Sleep 10
Send ^v
Sleep 10
Clipboard := clip
return

I imagine from what I saw trying to Google search this solution others will find this script useful also.

Thanks again,
FG

#5 Fanatic Guru

Fanatic Guru
  • Members
  • 48 posts

Posted 19 July 2012 - 09:49 PM

Keep in mind that what you and I call attachments and what Outlook calls attachments are different things. If you have images embedded in the body of an email, those are also considered attachments by Outlook even though they do not appear in the attachments section of the email as one typically expects.


I understand the "other" attachments issue.

That is not a problem since I only use this to list attachments in emails I am composing and I do not use any embedded images, backgrounds, borders or anything fancy like that.

If people do though, it is easier to delete the "false" attachments than type all the real attachments.

If the "false" attachments are consistent like a standard signature graphics or something they could be filtered from the list.

Also I wanted to think you for all your post dealing with COM.

I would have never got as far as I did without reading post from people like you and jethrow.

In my work environment getting AHK to interact with the suite of Microsoft Office products is very useful.

Thanks,
FG

#6 Fanatic Guru

Fanatic Guru
  • Members
  • 48 posts

Posted 02 May 2013 - 06:00 PM

Old thread but I thought I would update with the actual script I have been using with success for about a year now.

 

FG

; Email Attachment List
; Fanatic Guru
; 2013 04 26
;
; Based on code by jethrow and sinkfaze 
;
; Insert list of attachments in email being composed in Outlook.
;
;------------------------------------------------

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


; List Attachment Names in line with proper use of commas and "and"
#a::    ;<-- Add email attachment list as single line
   gosub GetAttachments
   gosub ListAttachments_Line
return

; List Attachment Names in column format with headings and variable underlining at the top
#^a::   ;<-- Add email attachment list table
   gosub GetAttachments
   gosub Calc_Underline
   if (AttachmentCount = 0) {
      Send ^u No Attached Files^+{Space}^u`n
   } else if (AttachmentCount = 1) {
      Send % "^u Attached File:" TabUnder "^u`n"
      gosub ListAttachments_Col
   } else {
      Send % "^u Attached Files:" TabUnder "^u`n"
      gosub ListAttachments_Col
   }
return

; List Attachment Names in column format with headings, numbers and variable underlining top and bottom
#!a::   ;<-- Add email attachment list table with file numbers
   gosub GetAttachments
   gosub Calc_Underline
   if (AttachmentCount = 0) {
      Send ^u No Attached Files^+{Space}^u`n
      return
   } else if (AttachmentCount = 1) {
      Send % "^u File {#} ^u{Tab}^u Attached File:" TabUnder "^u`n"
      gosub ListAttachments_Num
   } else {
      Send % "^u File {#} ^u{Tab}^u Attached Files:" TabUnder "^u`n"
      gosub ListAttachments_Num
   }
   Send % "^u{Tab}{Tab}" TabUnder "^u`n"
   Send Total Attached Files = %AttachmentCount% `n
return

GetAttachments:
   AttachmentNames:= Object()
   MailItem := ComObjActive("Outlook.Application").ActiveWindow.CurrentItem
   Attachments := MailItem.Attachments
   AttachmentCount := Attachments.Count
   Loop % AttachmentCount
      AttachmentNames[A_Index] := Attachments.item[A_Index].FileName
return

ListAttachments_Line:
   if (AttachmentCount = 0) {
      return
   } else if (AttachmentCount = 1) {
      Send % AttachmentNames[1] " "
   } else if (AttachmentCount = 2) {
      Send % AttachmentNames[1] " and " AttachmentNames[2] " "
   } else {
      Send % AttachmentNames[1]
      Loop % AttachmentCount-2
      {
         SendRaw % ", " AttachmentNames[A_Index+1]
      }
      SendRaw % " and " AttachmentNames[AttachmentCount] " "
   }
return

ListAttachments_Col:
      Loop % AttachmentCount
      {
         SendRaw % " " AttachmentNames[A_Index] "`n"
      }
return

ListAttachments_Num:
      Loop % AttachmentCount
      {
         SendRaw % "   " A_Index "`t" AttachmentNames[A_Index] "`n"
      }
return

Calc_Underline:
   MaxLen := 0
   Loop % AttachmentCount
   {
      if (StrLen(AttachmentNames[A_Index]) > MaxLen) {
         MaxLen := StrLen(AttachmentNames[A_Index])
      }

   }
   if (MaxLen < 15 ) {
      TabNum := 1
   } else {
      TabNum := Round(((MaxLen-15)/6)) + 1
   }
   TabUnder :=
   Loop % TabNum
   {
      TabUnder := TabUnder "`t"
   }
return