- Subject: Save Attachments to directory based on attachment name
- Author: whoslacks
- Date: 19 Jun 2008
- References:
Hello everyone - I am being a pain in the butt and if anyone does take
the time to help me out I will really apperciate it. (For what that
is worth, right) - that being said, as you are an experianced
programmer and I am a novice hack... tell me if this is something to
complex to be fishing for on the groups as I won't learn otherwise.
What I am looking for:
I want to create a macro that when run will take a look at all emails
in the inbox and save them to a subdirectory folder of C:\TD ; that
subdirectory will be determined by the name of the attachment.
Caveats:
I am on Exchange 2003 and Outlook 2007
The mailbox I want to process is not my primary Outlook account but
rather an additional exchange mailbox that I have added to my
account. For the purposes of example I will call my additional
mailbox box2 below.
The attachments are always going to be named 123456_Description.tif.
When the attachments are saved in the c:\TD folder I would like a
directory to be created named 123456 with the directory containing the
file 123456_Description.tif
One email could have multiple attachments.
I would then like email to move to the delete items folder after it is
processed.
Walking through the process:
Email comes into box2 inbox with two attachments 54454_docname.tif and
77745_docname_state.tif
Macro runs
Creates directories c:\TD\554454 and c:\TD\77745
Creates files c:\TD\54454\54454_docname.tif and c:\TD
\77745\77745_docname_state.tif
Email moved to box2 "Deleted Items"
Check for any additional emails in box2 inbox and repeat if necessary,
otherwise stop
One other caveat I just thought of, it is possible that the directory
could already exist as I could get a single email with two attachments
12345_mydoc.tif and 12345_otherdoc.tif In that instance I would like
the directory to be c:\TD\12345\ and have it contain both
12345_mydoc.tif and 12345_otherdoc.tif
My code so far:
Public Sub SaveTDAttachments()
Dim Inbox As MAPIFolder
Dim Item As MailItem
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Dim olns As Outlook.NameSpace
Set Inbox = olns.Folders("box2")
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\td\" & Atmt.FileName
Atmt.SaveAsFile FileName
Next
'Item.Delete
Next
Set Inbox = Nothing
End Sub
comments on my code:
1 . it doesn't work
2. I don't have the logic for the directory creation
3. I don't know how to tell it to look for an existing directory
4. I don't know how to tell it to create the directory based on the
attachment name text preceding the first _
5. I don't know if Item.Delete will move it to the box2 deleted items
folder or just delete it
6. I don't know how to tell it to look at box2 inbox
That is it... Thank you much!