我需要搜索電子表格中在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
但是,這並沒有任何工作。
我一直在嘗試,但我似乎無法使查找工作。我編輯了我在OP中測試的Find。 –