Download an attachment in an Outlook email (COM?)
Download an attachment in an Outlook email (COM?)
Do I have to use something like ObjMsg.Attachments?
Re: Download an attachment in an Outlook email (COM?)
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:
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)
}
Re: Download an attachment in an Outlook email (COM?)
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?
Re: Download an attachment in an Outlook email (COM?)
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.
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.
}
Re: Download an attachment in an Outlook email (COM?)
Interesting. What about the second question?
Re: Download an attachment in an Outlook email (COM?)
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.
Re: Download an attachment in an Outlook email (COM?)
Thanks but
isn't working.
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.
}
Re: Download an attachment in an Outlook email (COM?)
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?
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?
Re: Download an attachment in an Outlook email (COM?)
Email I've selected.doesn't work either.
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.
}
Re: Download an attachment in an Outlook email (COM?)
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)
}
}
}
}
; ==============================================================================================
Re: Download an attachment in an Outlook email (COM?)
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 translateto AHK.
Code: Select all
With Sheets("helpsheet").Range("a1").CurrentRegion
.Sort Key1:=Range("B1"), Order1:=xlDescending, Header:=xlYes
End With
Re: Download an attachment in an Outlook email (COM?)
Untested:(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: See Operators in Expressions, or there's a tutorial here: [VxE]'s := guide ? ( to the ternary ) : ( operator ) .
Code: Select all
xlApp.Sheets("helpsheet").Range("a1").CurrentRegion
.Sort(xlApp.Range("B1"), xlDescending := 2,,,,,, xlYes := 1)
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
Re: Download an attachment in an Outlook email (COM?)
Hi,
here is what I use with Outlook 2010, it works:
regards
J.B.
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
J.B.
AHK: 1.1.37.01 Ansi, 32-Bit; Win10 22H2 64 bit, german