Solved

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

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
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…
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…

729 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