【问题标题】:How to identify the item passed as a parameter using LastModificationTime?如何使用 LastModificationTime 识别作为参数传递的项目?
【发布时间】:2021-09-28 22:55:41
【问题描述】:

我有一个由 Outlook 规则触发的宏,它处理特定的传入电子邮件内容并将相关数据导出到 Excel 电子表格。
处理完电子邮件后,我想更改其类别并移至另一个文件夹。
在下面的代码中,对象“item”是给宏的电子邮件。

我的想法是使用 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”。我可以直接在传入的“项目”上执行我的任务。

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    首先,删除所有“on error resume next"”语句,以便您可以看到实际错误。

    其次,您假设Restrict() 至少返回一项。

    第三,切勿将“=”与 DateTime 值一起使用 - 由于舍入错误,该条件永远不会满足。始终使用范围 - ([LastModificationTime] > 'value1') AND ([LastModificationTime] < 'value2')

    【讨论】:

    • 谢谢德米特里。该代码没有任何“on error resume next”语句。我将 Restrict().item(1) 更改为 Restrict().GetFirst,但它没有用。我想要一个匹配,所以一个范围可能会失败。但我发现我的解决方法是在不搜索任何电子邮件的情况下完成我的任务,而是直接更改传入的“项目”MailItem
    • 如果您只想要一个匹配项,请改用 Items.Find - 它会更快。
    猜你喜欢
    • 1970-01-01
    • 2018-04-29
    • 2019-08-02
    • 2014-08-17
    • 2012-12-15
    • 2018-04-18
    • 2010-12-23
    • 1970-01-01
    • 2021-03-27
    相关资源
    最近更新 更多