+ Reply to Thread
Results 1 to 3 of 3

Check if email is unread in outlook then save attachment to folder

  1. #1
    Forum Contributor
    Join Date
    10-01-2013
    Location
    United States
    MS-Off Ver
    Microsoft version 365
    Posts
    110

    Check if email is unread in outlook then save attachment to folder

    I am trying to accomplish two things:

    I first want to check for a certain file in outlook and save it to a folder, then I want to make sure there are currently no files in the folder to avoid duplicates. So I want to delete files from my folder before saving the attachment. Here is the code below:


    Function Extract_Outlook_Email_Attachments()

    Dim OutlookOpened As Boolean
    Dim outApp As Outlook.Application
    Dim outNs As Outlook.NameSpace
    Dim outFolder As Outlook.MAPIFolder
    Dim outAttachment As Outlook.Attachment
    Dim outItem As Object
    Dim outMailItem As Outlook.MailItem
    Dim inputDate As String, subjectFilter As String
    Dim saveInFolder As String

    saveInFolder = "C:\Users\tzqy46\Documents\Test\" 'CHANGE FOLDER PATH AS NEEDED
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"

    ' inputDate = InputBox("Enter date to filter the email subject", "Extract Outlook email attachments")
    ' If inputDate = "" Then Exit Sub

    subjectFilter = "IMDS"

    'Get or create Outlook object and make sure it exists before continuing

    OutlookOpened = False
    On Error Resume Next
    Set outApp = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
    Set outApp = New Outlook.Application
    OutlookOpened = True
    End If
    On Error GoTo 0

    If outApp Is Nothing Then
    MsgBox "Cannot start Outlook.", vbExclamation
    Exit Function
    End If

    Set outNs = outApp.GetNamespace("MAPI")


    Set outFolder = outNs.PickFolder

    If Not outFolder Is Nothing Then
    For Each outItem In outFolder.Items
    If outItem.Class = Outlook.OlObjectClass.olMail Then
    Set outMailItem = outItem
    If outMailItem.Subject = subjectFilter Then
    Debug.Print outMailItem.Subject
    For Each outAttachment In outMailItem.Attachments
    outAttachment.SaveAsFile saveInFolder & outAttachment.FileName
    Next
    End If
    End If
    Next
    End If

    If OutlookOpened Then outApp.Quit

    Set outApp = Nothing

    End Function

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Check if email is unread in outlook then save attachment to folder

    Here is some code I put together it might work for you.
    - I use an Outlook filter rule to put the emails I want into a folder directly under Inbox.
    - I have a sub-folder under this folder called Processed"
    - I fill out a table with the Mailbox Name, Main Folder, SubFolder, where I want to save the attachments to and what kind of attachments to look for.

    Since the emails are moved from the main folder to the sub folder, they do not get processed twice.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Contributor
    Join Date
    10-01-2013
    Location
    United States
    MS-Off Ver
    Microsoft version 365
    Posts
    110

    Re: Check if email is unread in outlook then save attachment to folder

    Thank you so much. I will take a look at what you have to see if it will work for me.

+ 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. VBA - To save attachment from email to folder
    By narindran in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-27-2016, 11:35 PM
  2. [SOLVED] How to get outlook email attachment using excel vba and save it in a specified folder
    By jrtraylor in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-25-2016, 08:34 PM
  3. Posting to Outlook Public Folder as Unread
    By pjwhitfield in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-19-2015, 07:04 AM
  4. Save an Outlook attachment and move the email to a folder based and sender
    By db16886 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2014, 09:49 PM
  5. Dowload outlook email attachment to my pc folder using Excel VBA
    By din.malay in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-11-2013, 08:06 AM
  6. Dowload outlook email attachment to my pc folder
    By din.malay in forum Outlook Formatting & Functions
    Replies: 0
    Last Post: 12-11-2013, 03:34 AM
  7. Outlook, print pdf Save Attachment, Move Email To A Subfolder
    By stephen1000 in forum Outlook Programming / VBA / Macros
    Replies: 7
    Last Post: 11-16-2009, 07:18 PM

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