Solved

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

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
If you don't know how to downgrade, my instructions below should be helpful.
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 …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

13 Experts available now in Live!

Get 1:1 Help Now