Solved

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

Posted on 2016-11-20
2
55 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 50

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

726 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