+ Reply to Thread
Results 1 to 2 of 2

Macro to Rename Outlook Attachment Same as Email Subject

  1. #1
    Registered User
    Join Date
    Manila, Philippines
    MS-Off Ver
    Excel 2007

    Macro to Rename Outlook Attachment Same as Email Subject

    Hi Guys,

    Hope you're doing well. I got a code that downloads the attachments of unread outlook emails to a specified path. However, I would want to rename the attachment same as the email subject.

    Would really appreciate your inputs regarding this concern. Thanks in advance!

    Below would be my the codes I have.

    Sub SaveAttachments()

    Dim olApp As Outlook.Application, olNameSpace As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder, olMail As Outlook.MailItem
    Dim olAttachment As Outlook.Attachment, lngAttachmentCounter As Long

    On Error GoTo Oooops

    'Create an instance of Outlook and allow the user to choose
    'which folder they want to process
    Set olApp = New Outlook.Application
    Set olNameSpace = olApp.GetNamespace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders("Adhoc Reports")

    If olFolder Is Nothing Then Exit Sub 'User cancelled

    For Each olMail In olFolder.Items
    If olMail.UnRead = True Then 'If you want ALL messages processed then take this line out and the End If
    For Each olAttachment In olMail.Attachments
    lngAttachmentCounter = lngAttachmentCounter + 1
    olAttachment.SaveAsFile "C:\Users\U0171730\Sandy\Macro Automation Project\" & olAttachment.DisplayName
    Next olAttachment
    End If
    Next olMail

    AppActivate "Microsoft Excel"

    MsgBox lngAttachmentCounter & " attachments saved.", vbInformation, "Success!"

    Exit Sub


    MsgBox Err.Description, vbExclamation, "An error occurred"
    End Sub
    Last edited by sandy_314; 05-27-2013 at 10:57 AM.

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019

    Re: Macro to Rename Outlook Attachment Same as Email Subject

    Hi sandy

    Please add Code Tags to your Code. To do so, edit your Post, highlight the Code and press the # Icon (above).

    To assign the Email Subject to the File Name I think you need only to change this line of Code
    Please Login or Register  to view this content.
    However, this raises a couple other issues:
    • potential invalid characters in a file name
    • what happens when there's more than one attachment in the Email?
    • what file extension will you apply?
    These issues aren't insurmountable but need dealt with.

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1