Using Received in an AdvancedSearch does not give precise results

  • Thread starter Robert Davidson
  • Start date
R

Robert Davidson

When I use an AdvancedSearch with a received date criteria, the criteria is
not precise. It appears to subtract a couple hours.

When I enter it by hand in Customize View > Advanced, it works as expected.

If you run the below code in a folder where you keep messages that are close
in time, it should tell you that there are more message than exist that
arrived after the message you have selected.

While it should not make a difference, the folders I am searching
with have more than 2000 messages and less then 10,000. The folder is sorted
by received descending and I am reproducing by selecting one of the top 10
messages. I have tested against a PST store and an Exchange default folder
store.

Outlook 2002 (10.4219.4219) SP-2

Does anyone have any ideas on how to fix this - other than doing a for each
through the results and comparing the criteria date against the received
date again?

Best Regards,

Robert Davidson

Sub TestDateCriteria()
Dim SearchDate As Date
Dim sScope As String
Dim sFilter As String
Dim oSearch As Search

With Application.ActiveExplorer
sScope = "SCOPE ('shallow traversal of """ & .CurrentFolder.FolderPath &
"""')"
With .Selection.Item(1)
SearchDate = .ReceivedTime
End With
sFilter = "(""DAV:isfolder"" = false AND ""DAV:ishidden"" = false) AND "
sFilter = sFilter & "(""urn:schemas:httpmail:datereceived"" >= '" &
SearchDate & "')"
Debug.Print sFilter
Set oSearch = Application.AdvancedSearch(sScope, sFilter, False)
End With
Do Until oSearch.Results.Count > 0
DoEvents
DoEvents
Loop
MsgBox oSearch.Results.Count
End Sub
 
R

Robert Davidson

Here is code that uses For Each to return a corrected count. Note that performance is poor on a dial-up against an Exchange store. Sending HTML to avoid linewraps.

Sub TestDateCriteria()
Dim SearchDate As Date
Dim sScope As String
Dim sFilter As String
Dim oSearch As Search
Dim oItem As MailItem
Dim CorrectedCount As Long

With Application.ActiveExplorer
sScope = "SCOPE ('shallow traversal of """ & ..CurrentFolder.FolderPath & """')"
With .Selection.Item(1)
SearchDate = .ReceivedTime
End With
sFilter = "(""DAV:isfolder"" = false AND ""DAV:ishidden"" = false) AND "
sFilter = sFilter & "(""urn:schemas:httpmail:datereceived"" >= '" & SearchDate & "')"
Debug.Print sFilter
Set oSearch = Application.AdvancedSearch(sScope, sFilter, False)
End With
Do Until oSearch.Results.Count > 0
DoEvents
DoEvents
Loop

CorrectedCount = 0
For Each oItem In oSearch.Results
If oItem.ReceivedTime >= SearchDate Then
CorrectedCount = CorrectedCount + 1
End If
Next oItem

MsgBox "AdvancedSearch: " & oSearch.Results.Count & vbCrLf & "Corrected: " & CorrectedCount

End Sub
 
K

Ken Slovak - [MVP - Outlook]

Have you compared the generated SQL search string to the string you're generating?

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


Here is code that uses For Each to return a corrected count. Note that performance is poor on a dial-up against an Exchange store. Sending HTML to avoid linewraps.

Sub TestDateCriteria()
Dim SearchDate As Date
Dim sScope As String
Dim sFilter As String
Dim oSearch As Search
Dim oItem As MailItem
Dim CorrectedCount As Long

With Application.ActiveExplorer
sScope = "SCOPE ('shallow traversal of """ & ..CurrentFolder.FolderPath & """')"
With .Selection.Item(1)
SearchDate = .ReceivedTime
End With
sFilter = "(""DAV:isfolder"" = false AND ""DAV:ishidden"" = false) AND "
sFilter = sFilter & "(""urn:schemas:httpmail:datereceived"" >= '" & SearchDate & "')"
Debug.Print sFilter
Set oSearch = Application.AdvancedSearch(sScope, sFilter, False)
End With
Do Until oSearch.Results.Count > 0
DoEvents
DoEvents
Loop

CorrectedCount = 0
For Each oItem In oSearch.Results
If oItem.ReceivedTime >= SearchDate Then
CorrectedCount = CorrectedCount + 1
End If
Next oItem

MsgBox "AdvancedSearch: " & oSearch.Results.Count & vbCrLf & "Corrected: " & CorrectedCount

End Sub
 
R

Robert Davidson

Yes - I have tested with the generated SQL string in AdvancedSearch to verify.

The test script was designed to demonstrate the problem for anyone at any time.
Have you compared the generated SQL search string to the string you're generating?

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


Here is code that uses For Each to return a corrected count. Note that performance is poor on a dial-up against an Exchange store. Sending HTML to avoid linewraps.

Sub TestDateCriteria()
Dim SearchDate As Date
Dim sScope As String
Dim sFilter As String
Dim oSearch As Search
Dim oItem As MailItem
Dim CorrectedCount As Long

With Application.ActiveExplorer
sScope = "SCOPE ('shallow traversal of """ & ..CurrentFolder.FolderPath & """')"
With .Selection.Item(1)
SearchDate = .ReceivedTime
End With
sFilter = "(""DAV:isfolder"" = false AND ""DAV:ishidden"" = false) AND "
sFilter = sFilter & "(""urn:schemas:httpmail:datereceived"" >= '" & SearchDate & "')"
Debug.Print sFilter
Set oSearch = Application.AdvancedSearch(sScope, sFilter, False)
End With
Do Until oSearch.Results.Count > 0
DoEvents
DoEvents
Loop

CorrectedCount = 0
For Each oItem In oSearch.Results
If oItem.ReceivedTime >= SearchDate Then
CorrectedCount = CorrectedCount + 1
End If
Next oItem

MsgBox "AdvancedSearch: " & oSearch.Results.Count & vbCrLf & "Corrected: " & CorrectedCount

End Sub
 
K

Ken Slovak - [MVP - Outlook]

Does it work any better if you trap AdvancedSearchComplete and set a Boolean in that when the search is completed and you check that Boolean in your loop instead of checking for .Results.Count > 0?

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


Yes - I have tested with the generated SQL string in AdvancedSearch to verify.

The test script was designed to demonstrate the problem for anyone at any time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top