Download an attachment in an Outlook email (COM?)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
danotto94
Posts: 14
Joined: 11 Aug 2015, 10:32

Download an attachment in an Outlook email (COM?)

11 Aug 2015, 11:20

Do I have to use something like ObjMsg.Attachments?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Download an attachment in an Outlook email (COM?)

11 Aug 2015, 12:41

Yes, if ObjMsg is an Outlook MailItem object, the MailItem.Attachments property, "Returns an Attachments object that represents all the attachments for the specified item."

This will save all of an item's attachments to the desktop:

Code: Select all

for Attachment, in ObjMsg.Attachments
{
  Attachment.SaveAsFile(A_Desktop "\" Attachment.FileName)
}
danotto94
Posts: 14
Joined: 11 Aug 2015, 10:32

Re: Download an attachment in an Outlook email (COM?)

11 Aug 2015, 15:46

Thanks for replying. Why is it called Outlook.Attachment in http://stackoverflow.com/questions/1553 ... ename-them if it uses Outlook.MailItem? Also, how do you only download an attachment that has a specific string in its name?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Download an attachment in an Outlook email (COM?)

11 Aug 2015, 16:26

In VBA, programmers usually try to "dimension" (Dim - declare the type of) all variables. Otherwise each variable would need to be a Variant type, meaning it could accept any type of value. Variants are versatile, but they take up more memory than other types.

Dim objAtt As Outlook.Attachment - This line declares a variable called "objAtt" whose type is an object. Specifically an attachment object.
Outlook.MailItem is a different type of object. (A MailItem object)

In other words:
MailItem is a type of object. A MailItem object has a property called Attachments. The MailItem.Attachments property returns an Attachments object. The Attachments object is a collection of all the individual Attachment objects the MailItem object has.

Code: Select all

;...
SearchString := "abc"

; ObjMsg is a MailItem object that we have obtained previously
; MailItem objects have a property called "Attachments" which returns a collection of all the attachment objects for this mailItem
MyAttachments := ObjMsg.Attachments ;MyAttachments now contains an Attachments object.

; Now we can loop through each attachment.
; ie: for each Attachment in the collection...
for Attachment, in MyAttachments
{
  AttName := Attachment.FileName
  if (InStr(AttName, SearchString)) ; If the attachment file name contains the search string.
    Attachment.SaveAsFile(A_Desktop "\" AttName)  ; Save the attachment.
}
danotto94
Posts: 14
Joined: 11 Aug 2015, 10:32

Re: Download an attachment in an Outlook email (COM?)

11 Aug 2015, 16:45

Interesting. What about the second question?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Download an attachment in an Outlook email (COM?)

11 Aug 2015, 17:08

That's covered in the last couple lines of the code I posted above. I used InStr to check if 'AttName' contains 'SearchString.' Then it will only save the attachment if the text contained in the SearchString variable is present.
danotto94
Posts: 14
Joined: 11 Aug 2015, 10:32

Re: Download an attachment in an Outlook email (COM?)

12 Aug 2015, 12:34

Thanks but

Code: Select all

#3::
x := ComObjActive("Outlook.Application")
S := "Mailing" ;The actual attachment has other characters in the end.
A := MailItem.Attachments
 
; Now we can loop through each attachment.
; ie: for each Attachment in the collection...
for Attachment, in A
{
  N := Attachment.FileName
  if (InStr(N, S)) ;If the attachment file name contains the search string.
    Attachment.SaveAsFile(A_Desktop "\" N)  ;Save the attachment.
}
isn't working.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Download an attachment in an Outlook email (COM?)

12 Aug 2015, 12:48

You're missing the step where you get the mailitem object.
Where is this mailitem? Do you want to run this script on the email you have selected in outlook, or maybe on unread messages and/or in a certain folder?
danotto94
Posts: 14
Joined: 11 Aug 2015, 10:32

Re: Download an attachment in an Outlook email (COM?)

12 Aug 2015, 12:54

Email I've selected.

Code: Select all

#3::
O := ComObjActive("Outlook.Application")
S := "Mailing" ;The actual attachment has other characters in the end.
Se := O.ActiveExplorer.Selection
MailItem.Attachments := Se
 
; Now we can loop through each attachment.
; ie: for each Attachment in the collection...
for Attachment, in MailItem.Attachments
{
  N := Attachment.FileName
  if (InStr(N, S)) ;If the attachment file name contains the search string.
    Attachment.SaveAsFile(A_Desktop "\" N)  ;Save the attachment.
}
doesn't work either.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Download an attachment in an Outlook email (COM?)

12 Aug 2015, 13:17

Code: Select all

; ==============================================================================================
#3::
S := "Mailing"
olApp := ComObjActive("Outlook.Application")
olNamespace := olApp.GetNamespace("MAPI")
; Get the active window so we can determine if an Explorer or Inspector window is active.
Window := olApp.ActiveWindow 
if (Window.Class = 34)  ; 34 = An Explorer object. (The Outlook main window)
{
  Selection := Window.Selection
  if (Selection.Count != 0)
  {
    for Item, in Selection
    {
      if (Item.Class = 43)  ; 43 = A MailItem object.
      {
        SaveItemAttachments(Item,, S)
      }
    }
  }
}
else if (Window.Class = 35) ; 35 = An Inspector object. (The email is open in its own window)
{
  Item := Window.CurrentItem
  SaveItemAttachments(Item,, S)
}
return
; ==============================================================================================
SaveItemAttachments(Item, Folder := "", S := "")
{
  if (Item.Attachments.Count != 0)
  {
    for Attachment, in Item.Attachments
    {
      N := Attachment.FileName
      if (!S || Instr(N, S))  ; If S is blank, or if N contains S - Save the attachment.
      {
        ; If you didn't specify a folder, the desktop will be used by default.
        Attachment.SaveAsFile((Folder ? Folder : A_Desktop) "\" N)
      }
    }
  }
}
; ==============================================================================================
danotto94
Posts: 14
Joined: 11 Aug 2015, 10:32

Re: Download an attachment in an Outlook email (COM?)

14 Aug 2015, 11:30

Thanks. Where can I read about the Folder ? Folder : A_Desktop syntax? Also, I saw posts explaining how to sort sheets by currency, but I don't get how to translate

Code: Select all

With Sheets("helpsheet").Range("a1").CurrentRegion
    .Sort Key1:=Range("B1"), Order1:=xlDescending, Header:=xlYes
End With
to AHK.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Download an attachment in an Outlook email (COM?)

14 Aug 2015, 11:57

Untested:

Code: Select all

xlApp.Sheets("helpsheet").Range("a1").CurrentRegion
  .Sort(xlApp.Range("B1"), xlDescending := 2,,,,,, xlYes := 1)
(assumes xlApp contains a reference to an Application object)
xlDescending and xlYes are Excel constants. We need to look up their value (2 & 1) and use that in the script instead of their names.
"helpsheet" is the name of the sheet.

The "Folder ? Folder : A_Desktop" syntax is called "ternary syntax." It is just shorthand for an if/else statement. The following is equivalent:

Code: Select all

if (Folder)
  Var := Folder
else
  Var := A_Desktop
See Operators in Expressions, or there's a tutorial here: [VxE]'s := guide ? ( to the ternary ) : ( operator ) .
User avatar
Jovannb
Posts: 268
Joined: 17 Jun 2014, 02:44
Location: Austria

Re: Download an attachment in an Outlook email (COM?)

19 Aug 2015, 03:12

Hi,

here is what I use with Outlook 2010, it works:

Code: Select all

/*
Export all Attachments of a currently opened Outlook-Email to a folder
*/

#singleinstance force
#persistent

<^>!x::    ; AltGr-right-x as Hotkey!
gosub export
return


export:

; ###### Exporting of Email in current Outlook-Inspector
oOutlook := ComObjActive("Outlook.Application")
oInspector := oOutlook.Application.ActiveInspector
Mail := oInspector.CurrentItem ; thats the current Emails Object


; Exporting Attachments
att_count:=mail.attachments.count
loop, %att_count%
{
	att_filename:=mail.attachments(a_index).filename
	storepathname:="c:\temp\" att_filename
	mail.attachments(a_index).saveasfile(storepathname)
}
return	
regards

J.B.
AHK: 1.1.37.01 Ansi, 32-Bit; Win10 22H2 64 bit, german

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Descolada, GEOVAN and 140 guests