+ Reply to Thread
Results 1 to 8 of 8

Save outlook attachments and rename/append files with identifier from subject line

  1. #1
    Registered User
    Join Date
    08-02-2012
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Save outlook attachments and rename/append files with identifier from subject line

    Hi All,

    I'm a newbie to VBA and was hoping I can get some assistance from the masters..I currently have about a 2000+ e-mails with attachments to where I want to save those files and rename them with the subject line of the e-mail to my hard drive. I've been able to accomplish that with the below macro for the .XLS files (which I found on this forum), however I am unable to do the same activity with the .pdf files. The PDF files have all the same names (Report.1). Could that be an issue? Would appreciate any help! Apologies if I posted anything wrong.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,642

    Re: Save outlook attachments and rename/append files with identifier from subject line

    When you say; "however I am unable to do the same activity with the .pdf files.", what exactly happens. Do you get an error? If yes, what's the error description and what line is highlighted when you click the Debug button on the error dialog.

    Check to make sure the Email subject doesn't contain invalid file name characters
    A filename cannot contain any of the following characters:
    \ / : * ? " < > |
    Does the save path exist?
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-02-2012
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Save outlook attachments and rename/append files with identifier from subject line

    Thank you Alpha Frog. I just checked the Email subjects and yes, they contain colons...Here is an example of a subject line, but subject lines are not uniform in naming- ERF Site Name: NL-xxx: Project ID: Uxxx.2C.NL0xxx.1B.OEM.6601Dxx-ID.WA.GK1615.01132013 Callout: hK803741641C12 This is 1 of 1 ERFs for this callout

  4. #4
    Registered User
    Join Date
    08-02-2012
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Save outlook attachments and rename/append files with identifier from subject line

    Since the attachments all have the same name, how can I modify the code so that I can download and rename them to be in sequential order (Report_1, Report_2, Report_3....)?

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,642

    Re: Save outlook attachments and rename/append files with identifier from subject line

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-02-2012
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Save outlook attachments and rename/append files with identifier from subject line

    Alpha Frog, I might be missing something, but it seems that the files are being overwritten as they download to the folder. Out of some +1000 attachments, i've only been able to save about 9 attachments.


    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Sub Download_contracts(MyMail As MailItem)
    Dim strID As String
    Dim objNS As Outlook.NameSpace
    Dim objMail As Outlook.MailItem
    Dim objAtt As Outlook.Attachment
    Dim c As Integer
    Dim save_name As String
    'Place path to sav to on next line. Note that you must include the
    'final backslash
    Const save_path As String = "C:\Users\michael.dia\Documents\OLAttachments\"

    strID = MyMail.EntryID
    Set objNS = Application.GetNamespace("MAPI")
    Set objMail = objNS.GetItemFromID(strID)

    If objMail.Attachments.Count > 0 Then
    For c = 1 To objMail.Attachments.Count
    Set objAtt = objMail.Attachments(c)
    i = i + 1
    save_name = "Report_" & i & ".pdf"
    objAtt.SaveAsFile save_path & save_name


    Next
    End If

    Set objAtt = Nothing
    Set objMail = Nothing
    Set objNS = Nothing
    End Sub
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,642

    Re: Save outlook attachments and rename/append files with identifier from subject line

    Add this line...

    Please Login or Register  to view this content.
    NOTE: if you quit Excel then run this macro again later, it will start numbering the report files starting with #1

  8. #8
    Registered User
    Join Date
    08-02-2012
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Save outlook attachments and rename/append files with identifier from subject line

    Sweet! That did the trick, but the files are all corrupted and I can't open the PDF's. I'll try again..

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Download attachment and rename with subject line
    By ali84pk in forum Outlook Programming / VBA / Macros
    Replies: 11
    Last Post: 07-25-2023, 12:01 PM
  2. Download Attachment and Rename With Subject Line v2
    By CBJason in forum Outlook Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2014, 04:33 PM
  3. Replies: 0
    Last Post: 05-21-2014, 04:45 PM
  4. Extracting Attachments from outlook based on subject title /file name and between a date
    By himynameisiain in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-14-2013, 02:14 PM
  5. [SOLVED] Macro to Rename Outlook Attachment Same as Email Subject
    By sandy_314 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2013, 09:59 AM
  6. Extract and rename batch attachments from saved outlook files on desktop.
    By entice in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 03-01-2011, 11:49 PM
  7. Changing the Outlook subject line...
    By ChrisMattock in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2009, 10:19 AM

Bookmarks

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