Closed Thread
Results 1 to 12 of 12

Download attachment and rename with subject line

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

    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

    Please Login or Register  to view this content.
    Last edited by DonkeyOte; 12-18-2010 at 11:15 AM. Reason: added tags ... title edited: "Urgent Help!. " removed

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

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

    Try

    Please Login or Register  to view this content.
    Martin

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

    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 Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    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

    Please Login or Register  to view this content.
    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
    59

    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?


    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-04-2012
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Download attachment and rename with subject line

    No to revive this old thread, but I'm having issues running this with "MyMail As MailItem" between the brackets: Sub Download_contracts(MyMail As MailItem)

    I'm unable to step through the macro or see it in the "Alt F8" window. If I remove it from the brackets and declare the variable separately I get an error here after stepping through it: strID = MyMail.EntryID

    "Object variable or With block variable not set".

    Am I missing something? I do have my outlook references setup.

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Download attachment and rename with subject line

    rtnlsltn,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  9. #9
    Registered User
    Join Date
    03-10-2015
    Location
    Dublin
    MS-Off Ver
    2010
    Posts
    1

    Re: Download attachment and rename with subject line

    Hi,

    I have used the following script to achieve what you are doing BUT it only deals with 1 attachment per mail - either it doesnt save the second (and subsequent) attachments or else it saves them and overwrites them. Any ideas?
    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "C:\Users\chrisf\Desktop\Image Test\"
    MSN = Trim(itm.Subject)

    For Each objAtt In itm.Attachments
    If objAtt.FileName <> "image001.gif" Then
    objAtt.SaveAsFile saveFolder & "\" & itm.Subject & ".JPG"
    End If

    Set objAtt = Nothing
    Next
    End Sub

  10. #10
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,028

    Re: Download attachment and rename with subject line

    hi,
    Please open yr own thread and ask for help!
    Regards, John55
    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.

    ...enjoy -funny parrots-

  11. #11
    Registered User
    Join Date
    07-25-2023
    Location
    Poland
    MS-Off Ver
    365
    Posts
    2

    Re: Download attachment and rename with subject line

    Hi,

    I would like to ask for help. I am using Office 365 and tried to use this code to save attachments from Outlook and rename them as subject of email.
    However I encountered the problem.

    This VBA code is working:

    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "C:\Users\ppp\Desktop\Image Test"

    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "" & objAtt.DisplayName


    Set objAtt = Nothing
    Next
    End Sub

    However when I want to amend it so that it saves the pdf attachments with the subject of the email it is not working. File is being saved down as type: file (not PDF) and size is 0 KB. Additionaly it doesn't save down the file with the subject name but with the name of the attachment.

    VBA code which I am using
    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "C:\Users\ppp\Desktop\Image Test"
    MSN = Trim(itm.Subject)

    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "" & itm.Subject & ".PDF"

    Set objAtt = Nothing
    Next
    End Sub


    Can somebody advise me please? I spent few days on solving this and have no further idea how to solve it

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,780

    Re: Download attachment and rename with subject line

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

Closed 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.6.0 RC 1