WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE and Outlook
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Save attachments in Outlook

Keywords: 	 Save attachments in Outlook

Example from user:

The code demonstrates how to save file attachments in Outlook (not Outlook Express). Hope this helps.

Bill

Answer:

; Dump outlook messages (with attachments) to file

ArcPath="C:\emailarc\"

olFolderInbox=6 ;constant DO NOT CHANGE
MSapp = ObjectCreate ("Outlook.Application")
objNameSpace = MSapp.GetNamespace("MAPI")
objFolder = objNameSpace.GetDefaultFolder(olFolderInbox);  'Access the Inbox

objcount=objFolder.Items
ValCount=objcount.Count;checks number of messages in inbox!

;Loop thru each item in the inbox
For i = 1 to Valcount

	objItem=objFolder.Items(i) ;set current message 
	Body = objItem.Body 
	HTMLBody = objItem.HTMLBody
	;ReceivedByName = objItem.ReceivedByName 
	Subject = objItem.Subject
	WHOTo = objItem.To
	SentOn = objItem.SentOn
	SenderName = objItem.SenderName

	GoSub CleanNames

	If !DirExist(CleanPathName) then Dirmake(CleanPathName) 
	If HTMLBody == ""
		outfile=FileOpen(StrCat(CleanPathName, CleanSubject,".txt"), "WRITE")
		FileWrite(outfile, Body)
	Else
		outfile=FileOpen(StrCat(CleanPathName,CleanSubject,".htm"), "WRITE")
		FileWrite(outfile, HTMLBody)
	EndIf
	FileClose(outfile)

	GoSub ProcessAttachments
Next i

MSApp = 0

EXIT

:ProcessAttachments
Attachments = objItem.Attachments ; returns a numeric value of 33338
;Application = Attachments.Application
;AClass = Attachments.Class
ACount = Attachments.Count
;AParent = Attachments.Parent
;ASession = Attachments.Session
If ACount == 0 Then Return

For A = 1 to Acount
	Attachment = Attachments.Item(A)
	A_FileName = Attachment.FileName
	;A_PathName = Attachment.PathName ; Returns Blank?
	A_SaveAsFile = Attachment.SaveAsFile(StrCat(CleanPathName,A_FileName)) 
Next 
Return

:CleanNames ; Remove invalid character(s) from file/path
allowedchars='abcdefghijklmonpqrstuvwxyz0123456789~!@#$^&()_-,.=+{}[]'
CleanSenderName=StrClean(SenderName,allowedchars, " ", @FALSE,2) 
CleanSentOn=StrClean(SentOn,allowedchars, "", @FALSE,2)
If StrLen(CleanSentOn) > 8
	CleanSentOn=StrFix(CleanSentOn, " ", 8)
EndIf
CleanSubject=StrClean(Subject,allowedchars, " ", @FALSE,2) 
If CleanSenderName == "" then CleanSenderName = "No Sender Name"
If CleanSentOn == "" then CleanSentOn = "No SentOn Date"
If CleanSubject == "" then CleanSubject = "No Subject"
CleanPathName=StrCat(ArcPath, CleanSenderName, "\", CleanSentOn, "\")
Return



Article ID:   W14685
Filename:   Save file attachments in Outlook.txt
File Created: 2008:11:04:09:47:20
Last Updated: 2008:11:04:09:47:20