PDA

View Full Version : Save e-mail attachments to hard drive



JohnnyBravo
03-30-2006, 08:36 PM
I've got a steady stream of e-mail attachments coming my way tonight which I need to save to my hard drive. I've set up a rule to have them all moved to a folder in my INBOX called "Library Scans" based on a keyword.

The problem is that the attachments are all named as a "Scan001.TIF" file. (I have no control over that part). So obviously when I save the attachments to my local drive, I want to keep the filenames the same but just add a "a" or a "1" after it: like thus:

Scan0011.TIF
Scan0012.TIF
Scan0013.TIF
Scan0014.TIF
etc.

I found Killian's code iin the KB, but I'm not sure how to make it work. I keep getting an error message at the highlighted line.

'########################################################################## #####
'### Module level Declarations
'expose the items in the target folder to events
Option Explicit
Dim WithEvents TargetFolderItems As Items
'set the string constant for the path to save attachments
Const FILE_PATH As String = "C:\JC Administrative\"

'########################################################################## #####
'### this is the Application_Startup event code in the ThisOutlookSession module
Private Sub Application_Startup()
'some startup code to set our "event-sensitive" items collection
Dim ns As Outlook.NameSpace
'
Set ns = Application.GetNamespace("MAPI")
Set TargetFolderItems = ns.Folders.Item( _
"Personal Folders").Folders.Item("Temp").Items

End Sub

'########################################################################## #####
'### this is the ItemAdd event code
Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
'when a new item is added to our "watched folder" we can process it
Dim olAtt As Attachment
Dim i As Integer

If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
'save the attachment
olAtt.SaveAsFile FILE_PATH & olAtt.FileName

'if its an Excel file, pass the filepath to the print routine
If UCase(Right(olAtt.FileName, 3)) = "XLS" Then
PrintAtt (FILE_PATH & olAtt.FileName)
End If
Next
End If

Set olAtt = Nothing

End Sub

'########################################################################## #####
'### this is the Application_Quit event code in the ThisOutlookSession module
Private Sub Application_Quit()

Dim ns As Outlook.NameSpace
Set TargetFolderItems = Nothing
Set ns = Nothing

End Sub

I'm sure someone here knows a real quick solution, but I'm completely stumped!

Killian
03-31-2006, 01:28 AM
Hi JB,

the line where the code is failing defines the collection of items that is declared "WithEvents", so when a mailitem is added (a mail arrives in that folder), the event code fires, saving the attachment.

In the KB example, it's set to Personal Folders\Temp, so you just need to change that line to define your "Library Scans" folder

JohnnyBravo
03-31-2006, 10:51 AM
Yeah that's what I figured. The first time I tried to run it, I forgot to do that. But I did change the path names and folder names to my own setup and it still gave me an error message.

The little project I was working on is done now so I don't need it as urgently as I did when I first posted my message, but I'm still puzzled as to why it didn't work after I made the revisions to your code.

Killian
04-04-2006, 03:09 AM
AFAIK, the only reason the Set TargetFolderItems line would fail is if the folder you specify doesn't exist :dunno