I'm trying to mark messages, copied from within an Outlook subfolder to Inbox into a file system folder, as read.
Sub demo1()
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim path As String
Dim i As Integer, iUnred As Integer
Dim objUnreadItems As Object, m As Object, att As Object, Item As Object
Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.Folders.Item("[email protected]") ' folders of your current account
Set objFolder = objFolder.Folders("Inbox")
Set objFolder = objFolder.Folders("SubFolder")
Set objUnreadItems = objFolder.Items.Restrict("[UnRead] = True")
' Debug.Print objUnreadItems.Count
If objUnreadItems.Count = 0 Then
' MsgBox "NO objUnreadItems Email In Inbox" TODO: add to logfile
Else
For Each Item In objUnreadItems
'Debug.Print objUnreadItems.Count
path = "C:\temp\" & Item.Subject & ".msg"
Item.SaveAs path, olMSG
'Debug.Print Item.ConversationTopic
' Item.unRead = False
Next
For Each Item In objFolder.Items.Restrict("[UnRead] = True")
Debug.Print objUnreadItems.Count & ": " & Item.ConversationTopic
Item.unRead = False
Next
End If
End Sub
output looks like:
9: FA Report for RMA# 2832844
9: FA Report for RMA# 2828196
8: FA Report for RMA# 2827687
7: FA Report for RMA# 2827667
6: FA Report for RMA# 2832909
All messages show up in c:\temp but only five out of the nine messages are marked as read.
You are modifying (by setting the UnRead
property) the collection as you iterate over its elements. Replace for each
with a down loop:
for i = objUnreadItems.Count to 1 step -1
set item = objUnreadItems(i)
path = "C:\temp\" & Item.Subject & ".msg"
Item.SaveAs path, olMSG
Item.UnRead = False
next
You might also want to cleanup the file name by removing invalid (for a file name) characters, such as :
.