Search code examples
vbaoutlookoutlook-2003

Saving attachment in outlook in different folder


I found this code on internet where you can automatically save an attachment in a certain folder:

   Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Temp"
    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
        Set objAtt = Nothing
    Next
End Sub

Is there any way where I can save the attachment in different folder?

Thanks


Solution

  • Your sample code hardcodes the destination folder in the saveAttachtoDisk sub. You can pass this information via parameter into (a variation of) this sub:

    Public Sub saveAttachtoDisk(itm As Outlook.MailItem, saveFolder As String)
      Dim objAtt As Outlook.Attachment
      For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          Set objAtt = Nothing
      Next
    End Sub
    

    and call it like this:

    Dim saveFolder As String
    saveFolder = "C:\Some\Where\Else"
    saveAttachtoDisk itm, saveFolder
    ...
    saveAttachtoDisk otheritm, "d:\why\not\here"
    

    Of course, now the question is: How should these 'other folders' be determined? By user interaction? By a function applied to some Outlook/User/Main/System property?