Answered by:
Save Attachments Rule in Outlook

Question
-
I'm wanting to create a rule in Outlook that checks emails from a certain address for attachments, and if there are attachments, save them in a folder on my PC. Is there a way to do this? I've looked around the rules, but nothing is there. I know you can set a rule to run a script, but I don't know anything about code. I can't imagine the code would be too difficult, I just want to save any attachments from a certain person in a folder on my PC.
Any suggestions would be great.
Thanks.Friday, August 21, 2009 2:17 PM
Answers
-
Hi,
I apologize for the delayed reply:
Please refer to below VBA code which will save the attachment at given location whenever it receives an email from a person/DL for which the script is running.
Sub RunthisMacro(MyMail As MailItem)
Dim strID As String
If (MyMail.Attachments.Count > 0) Then
SaveEmailAttachmentsToFolder "Inbox", " ", "E:\VS2008 projects\store Data", MyMail
End If
Set objMail = Nothing
End Sub
Sub SaveEmailAttachmentsToFolder(OutlookFolderInInbox As String, _
ExtString As String, DestFolder As String, objMail As Outlook.MailItem)
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim MyDocPath As String
Dim I As Integer
Dim wsh As Object
Dim fs As Object
On Error GoTo ThisMacro_err
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
'Set SubFolder = Inbox.Folders(OutlookFolderInInbox)
'counter
I = 0
'Create DestFolder if DestFolder = ""
If DestFolder = "" Then
Set wsh = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
MyDocPath = wsh.SpecialFolders.Item("mydocuments")
DestFolder = MyDocPath & "\" & Format(Now, "dd-mmm-yyyy hh-mm-ss")
If Not fs.FolderExists(DestFolder) Then
fs.CreateFolder DestFolder
End If
End If
If Right(DestFolder, 1) <> "\" Then
DestFolder = DestFolder & "\"
End If
' Check each message for attachments and extensions
For Each Atmt In objMail.Attachments
FileName = DestFolder & objMail.SenderName & " " & Atmt.FileName
Atmt.SaveAsFile FileName
I = I + 1
Next Atmt
' Show this message when Finished
If I > 0 Then
MsgBox "You can find the files here : " _
& DestFolder, vbInformation, "Finished!"
Else
MsgBox "No attached files in your mail.", vbInformation, "Finished!"
End If
' Clear memory
ThisMacro_exit:
Set SubFolder = Nothing
Set Inbox = Nothing
Set ns = Nothing
Set fs = Nothing
Set wsh = Nothing
Exit Sub
' Error information
ThisMacro_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: SaveEmailAttachmentsToFolder" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume ThisMacro_exit
End Sub
Please let me know if you’ve any more questions.
Thanks,
Dhanashri[MSFT].
Disclaimer:
By using the following materials or sample code you agree to be bound by the license terms below and the Microsoft Partner Program Agreement the terms of which are incorporated herein by this reference. These license terms are an agreement between Microsoft Corporation (or, if applicable based on where you are located, one of its affiliates) and you. Any materials (other than the sample code) we provide to you are for your internal use only. Any sample code is provided for the purpose of illustration only and is not intended to be used in a production environment. We grant you a nonexclusive, royalty-free right to use and modify the sample code and to reproduce and distribute the object code from the sample code, provided that you agree: (i) to not user Microsoft’s name, logo, or trademarks to market your software product in which the sample code is embedded; (ii) to include a valid copyright notice on your software product in which the sample code is embedded; (iii) to provide on behalf of and for the benefit of your subcontractors a disclaimer of warranties, exclusion of liability for indirect and consequential damages and a reasonable limitation of liability; and (iv) to indemnify, hold harmless, and defend Microsoft, its affiliates and suppliers from and against any third party claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the sample code.
- Proposed as answer by DhanashriModerator Tuesday, August 25, 2009 8:16 PM
- Marked as answer by DhanashriModerator Wednesday, October 14, 2009 1:00 AM
Tuesday, August 25, 2009 8:16 PMModerator