This is my script for retrieving emails from Outlook inbox and store their attachments.
The problem is that is not working properly. It is able to collect the attachments from unread emails and store them into SavePath
, but somehow it is stopping when it has read three emails.
The strange thing is that it is able to collect the remaining emails when it is executed again, but not all remaining unread emails.
E.g: I have 6 unread emails at Inbox, the first time it is executed only 3 emails were read and their attachments were collected. The second time it is executed, it reads the next 2 emails. The third time it is executed, the remaining email was read.
I can't figure out what is happening and why not all emails are read at once.
Dim SavePath
Dim Subject
Dim FileExtension
SavePath = "C:\IN\"
Subject = "'Transfer File'"
FileExtension = "ARmessage"
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(6) 'Inbox
Set colItems = objFolder.Items
Set colFilteredItems = colItems.Restrict("[Unread]=true")
Set colFilteredItems = colFilteredItems.Restrict("[Subject] = " & Subject)
For Each objMessage In colFilteredItems
intCount = objMessage.Attachments.Count
If intCount > 0 Then
For i = 1 To intCount
' if right(Ucase(objMessage.Attachments.Item(i)),9) = FileExtension then
objMessage.Attachments.Item(i).SaveAsFile SavePath & _
objMessage.Attachments.Item(i).FileName
' End If
Next
objMessage.Unread = False
End If
Next