Solved

VBA to Save an Unread Outlook Message Attachment with a variable name based on a counter

Posted on 2016-11-20
2
48 Views
Last Modified: 2016-11-21
Attached is an Outlook VBA code I have tried to modify to look in my Outlook Inbox for any unread messages with the subject "CHI Paycodes By Department".  Set a counter for every iteration the Unread Message is found, save the attachment to a specified folder "C:\Path" and the name file:  "Kronos_"& the iteration # &".xls".

I will be getting 3 emails everyday with this subject and an excel attachment.

Let me know what I need to do to modify the attached.

Thank you.

glen
AttachEmail.txt
0
Comment
Question by:GPSPOW
2 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41895726
HI,

pls try
Sub Work_with_Outlook()

     Dim OlApp As Object
     Dim olNs As Outlook.NameSpace
     Dim Fldr As Outlook.MAPIFolder
     Dim myItem As Object
     Dim myAttachment As Outlook.Attachment
     Dim I As Long
     Dim olMail As Variant


     Set OlApp = Application
     Set olNs = OlApp.GetNamespace("MAPI")
     Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
     Set myTasks = Fldr.Items

  Set resultItems = myTasks.Restrict("[UnRead] = True AND [Subject] = 'CHI Paycodes By Department (Excel)'")
  If resultItems.Count > 0 Then
 
    For Each myItem In resultItems
        If myItem.Attachments.Count <> 0 Then
            For Each myAttachment In myItem.Attachments
            If InStr(myAttachment.DisplayName, ".xls") Then
                I = I + 1
                myAttachment.SaveAsFile "C:\Path\Kronos_" & I & ".xls"
                End If
            Next
        End If

    Next


For Each myItem In resultItems 
myItem.UnRead = False
Next

 
  End If

End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:GPSPOW
ID: 41896137
Thank you

Glen
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question