Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing email attachments in Outlook 2

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
Hi all

Assume emails have arrived with attachments and have been placed in a subfolder called Temp of the Outlook Inbox through the use of an Outlook rule.

How can one copy/extract or otherwise access those attachments programatically?

TIA
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Mike

The other way is to prevent an email that does not an attachment from being forwarded to the folder in the first place.

This 'filter' can be done through the Outlook rules wizard.

One remaining question is how to extract multiple attachments?

If that proves problematical, the client software can be such that only one attachment is ever attached to one email which is an easier option from a operational point of view anyway.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris

The old double loop thing.
Code:
On Error Do noattachment
Local lcFilename,lcPath
Public myError
lcPath="c:\savedattachments\"
oOutLookObject = Createobject("Outlook.Application")
olNameSpace = oOutLookObject.GetNameSpace("MAPI")
myAtts=olNameSpace.GetDefaultFolder(6).Folders("tEMP").Items
Set Step On
For Each loItem In myAtts
	For i = 1 To loItem.attachments.Count
		lcFilename=""
		lcFilename = loItem.attachments.filename
		If !myError
			lcFilename = Alltrim(lcPath)+lcFilename
			loItem.attachments.Item(1).SaveAsFile(lcFilename)
*loItem.Delete
		Endif
		myError = .F.
	Next
Next
Procedure noattachment
myError = .T.
Endproc
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first or check this link
 
Chris

You could also use the "loItem.attachments.Count" to step out of the loop if its "0" instead of generating and error if there are no attachment, just as a precaution, even though you are planning to use the message filtering.
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first or check this link
 
Here's my modified version of the code posted here...

Local lcFilename,lcPath, sFileExt, sFileObject
sFileExt = ".ZIP"
lcPath="c:\savedattachments\"
oOutLookObject = Createobject("Outlook.Application")
olNameSpace = oOutLookObject.GetNameSpace("MAPI")
myAtts=olNameSpace.GetDefaultFolder(6).Folders("subfolder").Items
for Each loItem In myAtts
lcFilename=""
IF TYPE("loItem.attachments.item(1).filename") != "U"
lcFilename = loItem.attachments.item(1).filename
IF sFileExt $ UPPER(lcFilename)
lcFilename = Alltrim(lcPath)+lcFilename
loItem.attachments.Item(1).SaveAsFile(lcFilename)
loItem.Delete()
ENDIF
endif
Next
Slighthaze = NULL
 
slighthaze

And how does your code handle the multiple attachments, which was Chris' latest request. And BTW why would want to limit your file saving to only zipped files? Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first or check this link
 
my apologies...there appears to be a lag at this end...did not see the other request and code until I had posted...as to the zip restriction I base it on the following Chris Thread quote...

The content of the email itself is not important - the client wants to receive information back from his field sales force and that information/data will be as an attachment in .zip format with an appropriate title to identity the contents of the .zip file. Slighthaze = NULL
 
Mike

You're still missing 'lcFileName = loItem.attachments.item(1).filename' in your code - other than that, between you and Slighthaze, you have a good FAQ shaping up here, especially if you resolve the issue of saving multiple attachments, although from a personal point of view, it's not that important.

What appeals to me is the way Outlook can become a 'hands free' portal enabling data to be shipped around by simply using its Rules facility.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
slighthaze

I guess, I would have gone with the:

IF JUSTEXT(UPPER(lcFilename))="ZIP".

But your way works also. Yes there is s slight lag, Dave advised me that they had to reboot the firewall.
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first or check this link
 
It would seem to me that you could use vfpcom to bind to Outlook's ItemAdd event and catch the attachments as the emails are brought down and placed in that subfolder...just a thought Slighthaze = NULL
 
Chris
Code:
local lcFilename,lcPath
lcPath="c:\savedattachments\"
oOutLookObject = Createobject("Outlook.Application")
olNameSpace = oOutLookObject.GetNameSpace("MAPI")
myAtts=olNameSpace.GetDefaultFolder(6).Folders("tEMP").Items
For Each loItem In myAtts
  IF loItem.attachments.count >0
    For i = 1 To loItem.attachments.Count
        lcFilename=""
        lcFilename = loItem.attachments.Item(1).filename
        If !myError
            lcFilename = Alltrim(lcPath)+lcFilename
            loItem.attachments.Item(1).SaveAsFile(lcFilename)
*loItem.Delete
        Endif
        myError = .F.
    Next
  ENDIF
Next

Why Chris, the above solution doesn't save multiple attachments? Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first or check this link
 
Chris

Sorry, it must have been a long day, here is the code to save mutliple attachments:
Code:
local lcFilename,lcPath
lcPath="c:\savedattachments\"
oOutLookObject = Createobject("Outlook.Application")
olNameSpace = oOutLookObject.GetNameSpace("MAPI")
myAtts=olNameSpace.GetDefaultFolder(6).Folders("tEMP").Items
For Each loItem In myAtts
  IF loItem.attachments.count >0
    For i = 1 To loItem.attachments.Count
        lcFilename=""
        lcFilename = loItem.attachments.Item(i).filename
        lcFilename = Alltrim(lcPath)+lcFilename
        loItem.attachments.Item(i).SaveAsFile(lcFilename)
        *loItem.Delete
    Next
  ENDIF
Next
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first or check this link
 
Mike

No - it only saves the first attachment according to the test data I am using, (also using your last code).

I have 4 emails - 2 with 1 attachment, 1 with 10, and 1 with 2.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Mike & Chris,

Double pass code works for me too Slighthaze = NULL
 
Mike and Slighthaze

Cross posting problem as ever!

The following, Mikes last version, works :-

'local lcFilename,lcPath
lcPath="c:\savedattachments\"
oOutLookObject = Createobject("Outlook.Application")
olNameSpace = oOutLookObject.GetNameSpace("MAPI")
myAtts=olNameSpace.GetDefaultFolder(6).Folders("tEMP").Items
For Each loItem In myAtts
IF loItem.attachments.count >0
For i = 1 To loItem.attachments.Count
lcFilename=""
lcFilename = loItem.attachments.Item(i).filename
lcFilename = Alltrim(lcPath)+lcFilename
loItem.attachments.Item(i).SaveAsFile(lcFilename)
*loItem.Delete
Next
ENDIF
Next'


Thanks both for your efforts - where's the FAQ?

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
I hereby nominate Mike to write the FAQ...anyone second that nomination? I felt like I was hanging onto his coattails with the wind whisping through my hair the way it was. Also, I am working on a VFPCOM solution that would add additional functionality to the code here...such as automatically pulling the attachments when the NewMail event happens...and the ability to see attachments, sender and open the attachments from a grid control...have some thoughts that treeview control may serve better than grid now. Slighthaze = NULL
 
slighthaze

Second your nomination that Mike should write the first FAQ, but also suggest you also create a FAQ from your VFPCOM solution with extended functionality.

Deal?
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris and Slighthaze

FAQ184-2838 is up. Made two changes:
1. Check to make sure the directory exists otherwise create it.
2. For a more generic function, I brought the searching to the Inbox.
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first or check this link
 
Mike,

Nice FAQ...and what a stroke of luck for me cause i have been having one heck of a time with retrieving attachments through Automation today and have been looking everywhere for a FAQ just like the one you wrote...very timely. :) Slighthaze = NULL
 
Mike

Good work - I trust it will appear in your upcoming tome on Outlook automation?
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top