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]
 
Chris

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.

Did you want to automate the process of saving the attachment also, or is that part already taking care of?
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first or check this link
 
you would have to hack the DBX file unless you wanted to use automation...I saved a simple email with a txt file containing "Hello World" attached in the drafts folder and then went and looked at the Drafts.dbx file and found the following...


Content-Type: text/plain;
name="a.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="MyText.txt"

HELLO WORLD


I looked around on the web and they haven't even got the entire dbx file format decoded yet from what I could see... Slighthaze = NULL
 
Mike

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.

So to answer your question, Yes.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Geez...are we talking about Microsoft Outlook? I thought we were talking about Outlook Express...I too would like to know how to add a subfolder to the inbox of Microsoft Outlook Slighthaze = NULL
 
Chris,

Could you clarify something for me...are you trying to do this by just hacking out the attachment or by using office automation and the Outlook object model? Slighthaze = NULL
 
Mike/Slighthaze

You can make a new folder through New > Folder on the Outlook menu.

It's also done through the Outlook Rules Wizard whereby you can divert incoming email to a folder of your choice.

FI, I have all incoming Tek-Teps thread notifications routed to the 'Inbox\Tek-Tips notifications' folder so they never appear in the Inbox.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris

I figured it out, thank you.

To start with, here is how to determine the name of the forlder, and this assumes there is only one (or its the first one).
Code:
oOutLookObject = CreateObject("Outlook.Application")
olNameSpace = oOutlookObject.GetNameSpace("MAPI")
Tempfolder=OlnameSpace.folders(1).Folders(2).Folders(1).Name

In my case the Inbox appears to be the second folder in Outlook. In order to determine the Inbox folder index use the following:
Code:
oOutLookObject = CreateObject("Outlook.Application")
olNameSpace = oOutlookObject.GetNameSpace("MAPI")
FOR i = 1 TO olNamespace.Folders(1).Folders(1).folders.count
  ? "The folder name is: "+olNamespace.Folders(1).Folders(i).Name, "folder number is: "+ALLTRIM(STR(i))
next

Well I'll keep plugging at it, I'll find a way to deal with the attachement.


Mike Gagnon

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

'are you trying to do this by just hacking out the attachment or by using office automation and the Outlook object model?'

I am hoping someone has been down this particular road and has an answer - I don't have any preconceptions as to how it might be done, but automation would be the first choice.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Mike

'I have question, is the attachment still attached to the message itself or just loose?'

This would be the first interface with the email after its receipt other than moving the emails to a suitable folder.

Once the attachments had been removed, the emails would need to be deleted - they simply act as a carrier for the attachments.

It also could be set up the way that suits best on the client laptops in the field.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Mike/slighthaze

The following is the VBA code from the article from the link mentioned by slighthaze.

At first glance it seem to be a question of accessing the attachments collection and saving those individual files to disk?

*!* Listing 1: The RunFile Subroutine

Sub RunFile(strFileName)
[tab]Set WshShell = CreateObject("WScript.Shell")
[tab]On Error Resume Next
[tab]WshShell.Run strFileName
[tab]If Err <> 0 Then
[tab][tab]strMsg = &quot;Could not open this file: &quot; & strFileName & _
vbCrLf & vbCrLf & &quot;You can use Windows Explorer's &quot; & _
&quot;Tools | Options | File Types to check what &quot; & _
&quot;program is associated with this type of file.&quot;
MsgBox strMsg, vbExclamation, &quot;Error opening file&quot;
[tab]End If
[tab]Set WshShell = Nothing
End Sub

*!* Listing 2: The OpenAllFiles Subroutine

Sub OpenAllFiles()
[tab]Dim objOL As Application
[tab]Dim objItem As Object
[tab]Dim colAtts As Attachments
[tab]Dim objAtt As Attachment
[tab]Dim fso As New Scripting.FileSystemObject
[tab]Dim strFile As String

[tab]Set objOL = CreateObject(&quot;Outlook.Application&quot;)
[tab]Set objItem = objOL.ActiveExplorer.Selection(1)
[tab]If Not objItem Is Nothing Then
[tab][tab]Set colAtts = objItem.Attachments
[tab][tab]If colAtts.Count > 0 Then
[tab][tab][tab]For Each objAtt In colAtts
[tab][tab][tab][tab]strFile = &quot;C:\Temp\&quot; & objAtt.FileName
[tab][tab][tab][tab]objAtt.SaveAsFile strFile
[tab][tab][tab][tab]Call RunFile(strFile)
[tab][tab][tab]Next
[tab][tab]End If
[tab]End If

[tab]Set fso = Nothing
[tab]Set objAtt = Nothing
[tab]Set colAtts = Nothing
[tab]Set objItem = Nothing
[tab]Set objOL = Nothing
End Sub
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris,

Here is the VFP equivalent of what you want:
Code:
LOCAL lcFilename,lcPath
lcPath=&quot;c:\savedattachments\&quot;
oOutLookObject = CreateObject(&quot;Outlook.Application&quot;)
olNameSpace = oOutlookObject.GetNameSpace(&quot;MAPI&quot;)
myAtts=oLNameSpace.GetDefaultFolder(6).Folders(&quot;tEMP&quot;).Items
FOR EACH loItem IN myAtts
   lcFilename=&quot;&quot;
   lcFileName = loItem.attachements.filename
   lcFileName = ALLTRIM(lcPath)+lcFileName
   loItem.attachements.item(1).SaveAsFile(&lcFilename)
   *loItem.Delete
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

Not wishing to be pedantic but :-

loItem.attachements.filename

Is that a typo?
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
ChrisRChamberlain

Not wishing to be pedantic but :-

loItem.attachements.filename

Is that a typo?


No, its French ;-), but being where you live you would use:
Code:
lcFileName = loItem.attachments.filename
lcFileName = ALLTRIM(lcPath)+lcFileName
loItem.attachments.item(1).SaveAsFile(&lcFilename)


Mike Gagnon

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

It's crashing at

'lcFileName = loItem.attachments.filename'

OLE error code 0x80020006 :Unknown name
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Mike

I suspect

lcFileName = loItem.attachments.filename

should be

lcFileName = loItem.attachments.item(1).filename

as it gives correct attachment filename, but now crashes on

loItem.attachments.item(1).SaveAsFile(&lcFilename)

which should be

loItem.attachments.item(1).SaveAsFile(lcFilename)

but then crashes on

lcFileName = loItem.attachments.item(1).filename

next time through the loop if there is not another attachment.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris

lcFileName = loItem.attachments.item(1).filename

Correct, if the e-mail has no attachment it throws an error, I would suggest you trap it.
Code:
On Error Do noattachment
Local lcFilename,lcPath
PUBLIC myError
lcPath=&quot;c:\savedattachments\&quot;
oOutLookObject = Createobject(&quot;Outlook.Application&quot;)
olNameSpace = oOutLookObject.GetNameSpace(&quot;MAPI&quot;)
myAtts=olNameSpace.GetDefaultFolder(6).Folders(&quot;tEMP&quot;).Items
for Each loItem In myAtts
	Set Step On
	lcFilename=&quot;&quot;
	lcFilename = loItem.attachments.filename
	If !myError
		lcFilename = Alltrim(lcPath)+lcFilename
		loItem.attachments.Item(1).SaveAsFile(&lcFilename)
*loItem.Delete
	Endif
	myError = .F.
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top