+ Reply to Thread
Results 1 to 6 of 6

Thread: Download attachment and rename with subject line

  1. #1
    Registered User
    Join Date
    10-21-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    50

    Download attachment and rename with subject line

    Dear Gurus

    I need urgent help, I have below macro to download attachment from email. But I need little amendment. I want to rename every attachment (in every email) with its subject line. My every email in outlook has only one attachment. I can't figure out how to do this trick.

    Thanks in Advance

    I am using this code to download all attachments

    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 = "D:\My Documents\Muhammad contracting\Contracts\"
    
    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)
    save_name = Left(objAtt.FileName, Len(objAtt.FileName) - 4)
    'save_name = save_name & Format(objMail.ReceivedTime, "_mm-dd-yyyy_hhmm")
    save_name = save_name & Right(objAtt.FileName, 4)
    objAtt.SaveAsFile save_path & save_name
    
    Next
    End If
    
    Set objAtt = Nothing
    Set objMail = Nothing
    Set objNS = Nothing
    End Sub
    Last edited by DonkeyOte; 12-18-2010 at 10:15 AM. Reason: added tags ... title edited: "Urgent Help!. " removed

  2. #2
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    3,005

    Re: Urgent Help!. Download attachment and rename with subject line

    Try

    save_name = objMail.Subject
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  3. #3
    Registered User
    Join Date
    10-21-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Urgent Help!. Download attachment and rename with subject line

    Dear Mrice

    Thanks for quick reply :-) it worked perfectly.

    Thanks a lot.

  4. #4
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Urgent Help!. Download attachment and rename with subject line

    @ali84pk - glad you have a resolution - going forward please be sure to familiarise yourself with the Forum Rules re: Code Tags, Titles etc...

  5. #5
    Registered User
    Join Date
    04-25-2012
    Location
    Alaska
    MS-Off Ver
    Microsoft Office 2007
    Posts
    1

    Re: Urgent Help!. Download attachment and rename with subject line

    Quote Originally Posted by mrice View Post
    Try

    save_name = objMail.Subject
    Hello,
    So where does this fit in the code above?

  6. #6
    Registered User
    Join Date
    10-21-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Urgent Help!. Download attachment and rename with subject line

    Quote Originally Posted by nika81 View Post
    Hello,
    So where does this fit in the code above?


    Sub Download_attachments_Sent_by_me2me(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:\Documents and Settings\AE1036\Desktop\Outlook attachments\"
    
    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)
    save_name = Left(objAtt.filename, Len(objAtt.filename) - 4)
    'save_name = save_name & Format(objMail.ReceivedTime, "_mm-dd-yyyy_hhmm")
    save_name = save_name & Right(objAtt.filename, 4)
    
    
    subject_name = objMail.Subject 'get subject
    correct_sub = Replace(subject_name, ":", "") ' remove bad characters
    objAtt.SaveAsFile save_path & correct_sub & ".pdf" ' save file on PATH with CORRECT SUB
    
    
    
    Next
    End If
    
    Set objAtt = Nothing
    Set objMail = Nothing
    Set objNS = Nothing
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0