2016-07-05 96 views
0

我需要搜索电子表格中在VBA中指定的范围之间给定的一些电子邮件标题。然后,我需要使用双向查找将SentOn时间粘贴到Excel中。获取电子邮件的发送日期,根据其主题和指定日期范围复制到Excel

我只能在当前日期这样做,因为当我在当前日期之前输入日期时,双向查找会粘贴今天电子邮件的SentOn日期。这让我觉得我已经搞砸了搜索Outlook。这里是我正在使用的(截断)代码:

Dim filterStr As String 
filterStr = "urn:schemas:httpmail:subject = '" & EmailName & "' AND urn:schemas:httpmail:date >= '" & TDateUTC & "' AND urn:schemas:httpmail:date <= '" & TDateUTCEOD & "' " 

For Each OutputType In ThisWorkbook.Worksheets("Static Data").Range("F:F") 
    If OutputType.value = "Email" Then 
     ProcessName = OutputType.Offset(0, -5).value 
     EmailName = OutputType.Offset(0, 2).value 

    On Error Resume Next 
     If Not (TargetInbox.Items.AdvancedSearch(TargetInbox, filterStr, False, "criteria") Is Nothing) Then 

     SLA_Completion_Tracker_FileName.Activate 
     MatchFormula1 = WorksheetFunction.Match(CLng(CDate(TDate)), ActiveSheet.Range("1:1"), 0) 
     MatchFormula2 = WorksheetFunction.Match(ProcessName, ActiveSheet.Range("A:A"), 0) 

     EmailTime = TargetInbox.Items.Item(EmailName).SentOn 
     If Not EmailTime >= TDate And EmailTime <= TDateEOD Then EmailTime = "" 

     SLA_Completion_Tracker_FileName.Activate 
     Set IndexFormula = WorksheetFunction.Index(ActiveSheet.Range("A1:FA60"), MatchFormula2, MatchFormula1) 

     IndexFormula.value = Format(EmailTime, "ddddd ttttt") 

End If 

这是目前正在为今天的日期。 然而,当我将其设置为搜索以前的日期,我替换此:

EmailTime = TargetInbox.Items.Item(EmailName).SentOn 
If Not EmailTime >= TDate And EmailTime <= TDateEOD Then EmailTime = "" 

有了这个:

EmailTime = TargetInbox.Items.AdvancedSearch(TargetInbox, filterStr, False, "criteria").Item(EmailName).SentOn 

什么也不显示。我意识到这是因为我没有正确使用AdvancedSearch函数,所以有人可以帮我解决这个问题吗?我如何正确使用它来完成这项任务?

感谢

编辑:我一直在试图找到为好,使用此代码:

EmailName = OutputType.Offset(0, 2).value 
Dim sFilter As String 
sFilter = "[Subject] = """ & EmailName & """ AND [SentOn] >= '" & Format(TDate, "ddddd h:nn AMPM") & "' AND [SentOn] <= '" & Format(TDateEOD, "ddddd h:nn AMPM") & "'" 
FoundMail = TargetInbox.Items.Find(sFilter) 
FoundTime = FoundMail.SentOn 

但是,这并没有任何工作。

回答

0

OP在这里,想通了我的问题。

这里就是我终于实现了,万一有人绊倒在此:

原来是最终很简单。

0

AdvancedSearch是异步的,你需要等待它完成。 为什么不直接使用MAPIFolder.Items.Find/FindNextItems.Restrict

+0

我一直在尝试,但我似乎无法使查找工作。我编辑了我在OP中测试的Find。 –