More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 23-May-2019 09:49

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

    Page [ 1 ]  
 Code Essentials Printer Friendly Version
Beginner’s section on how to work in the VBA environment, how to work with functions and expressions – generally not specific to Outlook
VBA Get Attachments Amendments
I have obtained some VBA code which saves all atatchedments in the Inbox to a folder on the hard disc.
I basically don't want it to copy all files in the inbox but a selected folder.

Could somebody amend the code to select a folder and not the indox please.

Code below

Option Explicit

'* Code based on sample code from Martin Green and adapted to my needs *

Sub GetAttachments()
On Error Resume Next
'create the folder if it doesnt exists:
    Dim fso, ttxtfile, txtfile, WheretosaveFolder
    Dim objFolders As Object
    Set objFolders = CreateObject("WScript.Shell").SpecialFolders
    'MsgBox objFolders("mydocuments")
    ttxtfile = objFolders("mydocuments")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set txtfile = fso.CreateFolder(ttxtfile & "\Email Attachments")
    Set fso = Nothing
    WheretosaveFolder = ttxtfile & "\Email Attachments"
On Error GoTo GetAttachments_err
' Declare variables
    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer
    Set ns = GetNamespace("MAPI")
    'Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    ' added the option to select whic folder to export
    Set Inbox = ns.PickFolder
    'to handle if the use cancalled folder selection
    If Inbox Is Nothing Then
                MsgBox "You need to select a folder in order to save the attachments", vbCritical, _
               "Export - Not Found"
        Exit Sub
    End If


    i = 0
' Check Inbox for messages and exit of none found
    If Inbox.Items.Count = 0 Then
        MsgBox "There are no messages in the selected folder.", vbInformation, _
               "Export - Not Found"
        Exit Sub
    End If
' Check each message for attachments
    For Each Item In Inbox.Items
' Save any attachments found
        For Each Atmt In Item.Attachments
        ' This path must exist! Change folder name as necessary.
           ' FileName = "C:\Email Attachments\" & Atmt.FileName
           'if want to add a filter:
           'If Right(Atmt.FileName, 3) = "xls" Then
           FileName = WheretosaveFolder & "\" & Atmt.FileName
            Atmt.SaveAsFile FileName
            i = i + 1
         Next Atmt
    Next Item
' Show summary message
    If i > 0 Then
        MsgBox "There were " & i & " attached files." _
        & vbCrLf & "These have been saved to the Email Attachments folder in My Documents." _
        & vbCrLf & vbCrLf & "Thank you for using Liron Segev - TheTechieGuy's utility", vbInformation, "Export Complete"
        MsgBox "There were no attachments found in any mails.", vbInformation, "Export - Not Found"
    End If
' Clear memory
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub
' Handle errors
    MsgBox "An unexpected error has occurred." _
        & vbCrLf & "Please note and report the following information." _
        & vbCrLf & "Macro Name: GetAttachments" _
        & vbCrLf & "Error Number: " & Err.Number _
        & vbCrLf & "Error Description: " & Err.Description _
        , vbCritical, "Error!"
    Resume GetAttachments_exit
End Sub

  15-May-2013  05:04
  15-May-2013  12:01   
Did you try it?

Inbox is a name only. It represents is the folder selected here.

Set Inbox = ns.PickFolder
    Page [ 1 ]