Solved

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

Posted on 2016-11-20
2
44 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now