WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: pauls681 on April 01, 2021, 06:44:07 AM

Title: Saving Outlook attachments from a folder other than inbox
Post by: pauls681 on April 01, 2021, 06:44:07 AM
I have the following code to copy attachments out of email in the inbox and it works.
What I want to do is copy attachments out of another folder.
How and where do i specify that outlook folder?
Any help is greatly appreciated.

AddExtender("wwfaf44i.dll")
olFolderInbox=6
SaveTo = "C:\docstoprint\"
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
Title: Re: Saving Outlook attachments from a folder other than inbox
Post by: td on April 01, 2021, 09:47:38 AM
Not sure why you loading the FAF extender when the script you posted doesn't use it but maybe it is a snippet from a larger script.

If you are lucky your folder is one of the default folders listed in the OLDefaultFolders enumeration. You can find the complete list using the WIL Type Viewer or performing a quick Web search on the term "OLDefaultFolders".  If you find it there, you just need to use the enumeration's value as the parameter to the "GetDefaultFolder" method. If you are not lucky you will likely have to use the "GetFolderFromID" or something similar but I am not sure how to go about determining the entryID or the storeID of an email message or a folder.

Perhaps someone more familiar with the Outlook object model can provide additional assistance.
Title: Re: Saving Outlook attachments from a folder other than inbox
Post by: td on April 01, 2021, 09:56:44 AM
Happened to notice that the Outlook Namespace object has a "Folders" property that returns a collection of folder objects.  With a little trial and error and an examination of the object model you should be able to sort it out.

Here is a Tech DB example that enumerates sub folders it may be of some help.

https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP/OLE~and~Outlook+List~Subfolders.txt

There may be other examples in the Tech DB that are useful so it is worth a search.
Title: Re: Saving Outlook attachments from a folder other than inbox
Post by: pauls681 on April 01, 2021, 10:26:46 AM
Thank you, I will investigate more and report back when I get this working.
On the faf extender... You are correct that there is more code doing some file manipulation that the faf extender is needed for.
Thank you so much for your help.
Title: Re: Saving Outlook attachments from a folder other than inbox
Post by: pauls681 on April 01, 2021, 10:42:05 AM
Got it!
oFolder = oNameSpace.GetDefaultFolder(olFolderInbox).Folders("Invoice")
Invoice is a sub folder under inbox, before it was not. This should work great for what I'm doing.
Thank you for your help!