+ Reply to Thread
Results 1 to 7 of 7

Downloading outlook attachment with specific subject line

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-30-2017
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2013
    Posts
    251

    Downloading outlook attachment with specific subject line

    I realize that there are a number of examples of this, but for some reason I'm having issues adjusting them to my own needs.

    My goal is simple. Within a specific folder (subfolder?), find email with matching subject line, create a folder in C: with the date of the email, and download the email attachment to the just created folder.

    The newly created folder piece hasn't been included yet because a) not sure exactly how to go about doing that and b) I can't seem to save the simple attachment to an already created test folder.

    Any help MUCH appreciated!

    Thank you!

    Here's what I have so far:

    Sub DownloadAttachment()
     
    Dim ns As Namespace
    Dim Inbox As MAPIFolder
    
    Dim Item As Outlook.MailItem
    Dim Atmt As Attachment
    Dim FileName As String
    
    Set ns = Outlook.GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox).Parent.Folders("Personal").Folders("Fund")
    
    
    For Each Item In Inbox.Items
        If Item.Subject = "HELLO THIS IS A TEST" Then
            FileName = "C:\Test\Attachments\" & Atmt.FileName
            Atmt.SaveAsFile FileName
        End If
    Next Item
    
    End Sub

  2. #2
    Forum Contributor
    Join Date
    06-30-2017
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2013
    Posts
    251

    Re: Downloading outlook attachment with specific subject line

    oops. Also, there will only be one attachment so there's no need to loop through all attachments of the email as I've seen folks do before.

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,961

    Re: Downloading outlook attachment with specific subject line

    Option Explicit
    
    Sub DownloadAttachment()
    Dim ns As Namespace
    Dim Inbox As MAPIFolder
    Dim Item As Object, Atmt As Object
    Dim FileName As String, Fld As String, Dt As String
    Set ns = Outlook.GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox).Folders("Personal").Folders("Fund")
    For Each Item In Inbox.Items
        If Item.Subject = "HELLO THIS IS A TEST" Then
            For Each Atmt In Item.Attachments
                Fld = "C:\Test\Attachments\"
                If Len(Dir(Fld, vbDirectory)) = 0 Then MkDir Fld
                Dt = Format(Item.ReceivedTime, "ddmmyyyy")
                If Len(Dir(Fld & "\" & Dt, vbDirectory)) = 0 Then MkDir Fld & "\" & Dt
                FileName = Fld & "\" & Dt & "\" & Atmt
                Atmt.SaveAsFile FileName
                Exit For
            Next Atmt
        End If
    Next Item
    End Sub
    Last edited by Sintek; 01-01-2020 at 07:57 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  4. #4
    Forum Contributor
    Join Date
    06-30-2017
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2013
    Posts
    251

    Re: Downloading outlook attachment with specific subject line

    thank you and happy new year! sorry for late reply, I really do appreciate the help (just been on vacation). Since I'm still new, how would i incorporate another email subject for which I want to download attachments? E.g.,
    If Item.Subject = "HELLO THIS IS A TEST" Or "HELLO THIS IS A TEST PT 2" Then

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,961

    Re: Downloading outlook attachment with specific subject line

    If Item.Subject = "HELLO THIS IS A TEST" Or Item.Subject = "HELLO THIS IS A TEST PT 2" Then

  6. #6
    Forum Contributor
    Join Date
    06-30-2017
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2013
    Posts
    251

    Re: Downloading outlook attachment with specific subject line

    This is perfect, thank you!

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,961

    Re: Downloading outlook attachment with specific subject line

    ...........................................
    Thanks.gif

+ 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. Modify VBA to save attachment with Subject line as file name??
    By dwhite30518 in forum Outlook Formatting & Functions
    Replies: 2
    Last Post: 02-27-2017, 11:32 AM
  3. Outlook Macro to save mail attachment from a specific sender with subject line
    By manoj.0790 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-17-2016, 09:34 AM
  4. Download Attachment from outlook by searching subject
    By lakshya2425 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-01-2015, 09:38 AM
  5. 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
  6. Replies: 0
    Last Post: 05-21-2014, 04:45 PM
  7. [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

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