问题描述
这里不是编码专家...但是: 我有一个由Outlook规则触发的宏,该规则处理特定的传入电子邮件内容并将相关数据导出到Excel电子表格。处理完电子邮件后,我想更改其类别并移动到另一个文件夹。 在下面的代码段中,对象“项目”是发送给宏的电子邮件。因此,我的想法是使用LastModificationTime属性作为“ item.LastModificationTime”来查找原始Outlook.MailItem并执行操作。 似乎代码在带有“ aux1”或“ aux2”过滤器的“ Set myItem = myItems.Restrict(aux2).item(1)”处停止。我没有弹出任何错误,但该行之后的所有命令均未执行。
Sub ExportToExcel(item As Outlook.MailItem)
Dim aux As String
Dim aux2 As String
Dim myInBox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Outlook.MailItem
'(irrelevant code that does email processing and excel spreadsheet writing...)
Set myInBox = Session.GetDefaultFolder(olFolderInBox)
Set myItems = myInBox.Folders("BACKUP").Items
MsgBox (myItems.item(1).Subject) 'Runs OK
aux = "[LastModificationTime] = """ & Format(item.LastModificationTime,"ddddd hh:nn") & """" 'just another try
aux2 = "[LastModificationTime] = """ & item.LastModificationTime & """"
MsgBox (aux) 'Runs OK
MsgBox (aux2) 'Runs OK
Set myItem = myItems.Restrict(aux2).item(1) 'Seems the code stops here with no error
MsgBox ("Here") 'Does NOT run from here on
MsgBox (myItem.Subject)
myItem.Categories = "Categoria Laranja"
Set myDestFolder = myInBox.Folders("BACKUP_Processed")
myItem.Move (myDestFolder)
End Sub
编辑:我意识到我不需要“ myItem” ...并且我可以直接在传入的“ item”上实现我的任务...这样就可以了!
解决方法
首先,删除所有可以看到实际错误的“ on error resume next"
”语句。
第二,假设Restrict()
返回至少一项。
第三,切勿将DateTime值与“ =”一起使用-由于舍入错误,该条件将永远无法满足。始终使用范围-([LastModificationTime] > 'value1') AND ([LastModificationTime] < 'value2')