PDA

View Full Version : saving outlook attachments to a folder



Analyst3
06-18-2014, 11:00 AM
Hello all,

So i get an email from a particular email address everyday( the time varies) how do i create a macro that'll take the attachment from the email and save it to another folder. This is what i have so far i am having an issue with this line "ns.Folders("C&Co Folder").Folders.Item("Inbox")". The C&Co folder is where the new email is saved and its under the inbox


'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 = "R:\Operations\ExposureReports\CF&CO Daily exposure Reports 2014"


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("C&Co Folder").Folders.Item("Inbox")
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
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

westconn1
06-19-2014, 03:18 AM
The C&Co folder is where the new email is saved and its under the inbox
looks like round the wrong way,
try

Set TargetFolderItems = GetNamespace("mapi").GetDefaultFolder(olFolderInbox).Folders("c&Co folder")


olAtt.SaveAsFile FILE_PATH & olAtt.FileName possibly you need a \ between file_path and filename, or add to your constant