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 outlook attachments

Question:

I have a problem with downloading an attachments from outlook. When I use the following code, it always downloads the whole message and not just the attachment in it.

This is what I tried:

olFolderInbox=6

MSapp = ObjectCreate ("Outlook.Application")
oNameSpace = MSapp.GetNamespace("MAPI")
oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
ofolderitems=oFolder.Items

For a = 1 to ofolderItems.count
oMessage = oFolder.Items(a)
if oMessage.senderName == "Sender"
Attachments = oMessage.Attachments
Attachment = Attachments.Item(1)
fname = Attachment.filename
Attachment.SaveAsFile("C:\windows\desktop\%fname%")
break
endif
Next
Any suggestions?

Answer:

This code seems to work for me:

olFolderInbox=6
SaveTo = "C:\windows\desktop\"
MSapp = ObjectCreate ("Outlook.Application")
oNameSpace = MSapp.GetNamespace("MAPI")
oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
oFolderItems=oFolder.Items
msgcount = oFolderItems.count
For a = 1 to msgcount
	oMessage = oFolderItems.Item(a)
	oAttachments = oMessage.Attachments
	attachcount = oAttachments.Count
	For b = 1 to attachcount
		oAttachItem = oAttachments.Item(b)
		fname = oAttachItem.filename
		filetosave = StrCat(SaveTo,fname)
		oAttachItem.SaveAsFile(filetosave)
		oAttachItem = 0
	Next
	oAttachments = 0
   oMessage = 0
Next



oFolderItems = 0
oFolder = 0
oNameSpace = 0
MSapp = 0


Article ID:   W16095
File Created: 2009:04:01:10:30:50
Last Updated: 2009:04:01:10:30:50