Consulting

Results 1 to 4 of 4

Thread: Save email attachment and prefix with part of email subject

  1. #1
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    3
    Location

    Save email attachment and prefix with part of email subject

    Hi all,

    I'm new to the forum and this is my first post.

    I have been given a piece of code for saving an attachment to a local folder if the email is moved into a specific folder in outlook. I've seen similar code on this forum so that may be where they got it from in the first place; if so, all credit goes to them.

    Option Explicit
    Dim WithEvents TargetFolderItems As Items
    
    
    Const FILE_PATH As String = "\\tcc-fileserver\data extracts$\Engage Hub\"
     
    Private Sub Application_Startup()
    
    
        Dim ns As Outlook.NameSpace
         
        Set ns = Application.GetNamespace("MAPI")
        Set TargetFolderItems = ns.GetDefaultFolder(olFolderInbox).Folders("Extracts").Items
         
    End Sub
     
    
    Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
    
    
        Dim olAtt As Attachment
        Dim i As Integer
         
        If Item.Attachments.Count > 0 Then
            For i = 1 To Item.Attachments.Count
                Set olAtt = Item.Attachments(i)
                olAtt.SaveAsFile FILE_PATH & olAtt.FileName
            Next
        End If
         
        Set olAtt = Nothing
         
    End Sub
     
    Private Sub Application_Quit()
         
        Dim ns As Outlook.NameSpace
        Set TargetFolderItems = Nothing
        Set ns = Nothing
         
    End Sub
    What I would ideally like to do is:
    1. Move all emails which contain EngageHub to a specific folder in Outlook (Inbox/Extracts). I can do this with rules if it is not worth doing in VBA
    2. Prefix the attachment name with the beginning of the Email Subject (the Email subject will contain the group name which will usually be formatted as GroupName1_EngageHub_ddMMyyyy. The attachment is usually just called EngageHub_ddMMyyyy.
    3. Download the attachment to a local folder ("\\tcc-fileserver\data extracts$\Engage Hub"). The above code can do this for me.


    My main issue is referencing the Outlook message subject. Any help would be greatly appreciated.

  2. #2
    The following should work, subject to there being no illegal filename characters in the groupname.

    olAtt.SaveAsFile FILE_PATH & Split(Item.Subject, "_")(0) & "_" & olAtt.fileName
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    3
    Location
    Thanks Graham. Works perfectly.
    Is there any way to reference the time that the email was created in the filename as well? For example GroupName1_EngageHub_[Date message created]?

  4. #4
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    3
    Location
    Nevermind, found this out myself (Item.CreationTime). Thanks for your help!

Posting Permissions

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