如何遍历文件夹中的项目?
How to loop through items in a folder?
CONTEXT: 我试图首先检查文件夹中的邮件(“待处理”)是否已被读取。如果邮件已读,将其移动到另一个文件夹(“完成”)。
然后,对于“待处理”中的剩余邮件,保存附件并将邮件标记为“已读”。
问题: 当我尝试遍历文件夹中的项目时,它会掉落
Run-time error 438:
The object doesn't support this property or method
代码:
Sub MoveInbox2Reviewed()
Dim OutlookApp As Outlook.Application
Dim ONameSpace As Object
Dim OItem As Outlook.MailItem
Dim OFolderSrc As Object
Dim OFolderDst As Object
Dim Path As String
Set OutlookApp = New Outlook.Application
Set ONameSpace = OutlookApp.GetNamespace("MAPI")
Set OFolderSrc = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("pending")
Set OFolderDst = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("done")
Path = "C:\Users\..."
For Each OItem In OFolderSrc
If OItem.UnRead = False Then
OItem.Move OFolderDst
End If
Next
For Each OItem In OFolderSrc
OItem.Attachments.SaveAsFile Path & Attachment.DisplayName
OItem.UnRead = False
Next
End Sub
首先,您正在循环浏览文件夹。不是物品。你需要 OFolderSrc.Items
.
其次,你不应该遍历文件夹中的所有项目,使用 Items.Restrict
:
For Each OItem In OFolderSrc.Items.Restrict("[Unread] = true")
CONTEXT: 我试图首先检查文件夹中的邮件(“待处理”)是否已被读取。如果邮件已读,将其移动到另一个文件夹(“完成”)。
然后,对于“待处理”中的剩余邮件,保存附件并将邮件标记为“已读”。
问题: 当我尝试遍历文件夹中的项目时,它会掉落
Run-time error 438:
The object doesn't support this property or method
代码:
Sub MoveInbox2Reviewed()
Dim OutlookApp As Outlook.Application
Dim ONameSpace As Object
Dim OItem As Outlook.MailItem
Dim OFolderSrc As Object
Dim OFolderDst As Object
Dim Path As String
Set OutlookApp = New Outlook.Application
Set ONameSpace = OutlookApp.GetNamespace("MAPI")
Set OFolderSrc = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("pending")
Set OFolderDst = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("done")
Path = "C:\Users\..."
For Each OItem In OFolderSrc
If OItem.UnRead = False Then
OItem.Move OFolderDst
End If
Next
For Each OItem In OFolderSrc
OItem.Attachments.SaveAsFile Path & Attachment.DisplayName
OItem.UnRead = False
Next
End Sub
首先,您正在循环浏览文件夹。不是物品。你需要 OFolderSrc.Items
.
其次,你不应该遍历文件夹中的所有项目,使用 Items.Restrict
:
For Each OItem In OFolderSrc.Items.Restrict("[Unread] = true")