AdvancedSearch behavior

W

Wild Bill

Below I attempt a search that should succeed, which returns
results.count=0.
Then I attempt a search that should fail; it returns results.count=0.
Then I try to search folder "Deleted Items" which always gives "The
Operation Failed" and Err numbers that vary wildly, though apparently
always negative, e.g. -1386805681, -1352202673, -1317599665 (It does
seem that it's been decreasing as the evening has progressed??!!).

The 1st and 3rd answers bewilder me, and I hoped you could help. BTW, I
am ignorant about "urn:schemas" and confess to have snagged this from
other n/g posts; I assume that it lets me search the subject field.
Worse yet, this is my first time using AdvancedSearch so forgive my
ignorance. I am clear on using Application_AdvancedSearchComplete as
sometimes shown in the n/g, but want to be comfortable with the above
questions first.

OL03. Both noted folders have items as I run this. Neither has a
subfolder.

Sub testSearch()
Dim objSch As Search, strF As String

strF=Chr(34) & "urn:schemas:httpmail:subject"" like 'News'"
Set objSch = Application.AdvancedSearch _
(Scope:="Inbox", Filter:=strF, SearchSubFolders:=True) 'should work
Debug.Print objSch.Results.Count

strF=Chr(34) & "urn:schemas:httpmail:subject"" =
'ToStringTheImpossibleString'"
Set objSch = Application.AdvancedSearch _
(Scope:="Inbox", Filter:=strF, SearchSubFolders:=True) 'should fail
Debug.Print objSch.Results.Count

Set objSch = Application.AdvancedSearch _
(Scope:="Deleted Items", Filter:=strF, SearchSubFolders:=True)
End Sub
 
S

Sue Mosher [MVP-Outlook]

When you're using the Like operator, you need to include wildcards in the text you're searching for, e.g.:

strF=Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " Like '%News%'"

I don't know why you're getting an error in searching Deleted Items.
 
K

Ken Slovak - [MVP - Outlook]

That is the correct DASL property tag for Subject (DASL is a query language
used for Advanced Search, among other things).

For the second search I'd write the search term this way to avoid errors:
strF = Chr(34) & "urn:schemas:httpmail:subject"" =
'ToStringTheImpossibleString'"

I would expect no results at all for your debug.print statements, the
AdvancedSearch method call returns right away and you get your results
asynchronously in the AdvancedSearch.Complete event handler.

For your final search term use something like this, always putting single
quotes around any folder name that has a space in it:

Set objSch = Application.AdvancedSearch _
(Scope:="'Deleted Items'", Filter:=strF, SearchSubFolders:=True)
 
W

Wild Bill

Thank you and Sue for each providing valuable specific insightful
instruction. I'll try each idea now.

I had a latency problem so if I can't remove it in time, I reposted the
question - please ignore it with my apologies.
 
W

Wild Bill

Yes, ' around folder name 'Deleted Items' was the salvation. And the
asynchronous explanation for the count makes PERFECT sense. And
doohhhhh of course without % the Like was useless. Thank you, thank
you, thank you.

A couple of new questions (for anyone): I understand you can run a
boatload of these async. processes, so I ran all three and *then* went
Test... a la

Public blnSearchComp As Boolean
Sub testSearchAsPostedInNG()
Dim objSch As Search, strF As String

strF = Chr(34) & "urn:schemas:httpmail:subject"" like '%News%'"
'should succeed
Set objSch = Application.AdvancedSearch(Scope:="'Inbox'",
Filter:=strF, SearchSubFolders:=True)
'TestAdvancedSearchComplete 'commented to try doing 3 at once
Debug.Print objSch.Results.Count

strF = Chr(34) & "urn:schemas:httpmail:subject"" =
'ToStringTheImpossibleString'" 'should fail
Set objSch = Application.AdvancedSearch(Scope:="'Inbox'",
Filter:=strF, SearchSubFolders:=True)
'TestAdvancedSearchComplete 'commented to try doing 3 at once
Debug.Print objSch.Results.Count

Set objSch = Application.AdvancedSearch(Scope:="'Deleted Items'",
Filter:=strF, SearchSubFolders:=True)
TestAdvancedSearchComplete
Debug.Print objSch.Results.Count
End Sub
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As
Search)
MsgBox "The AdvancedSearchComplete Event fired"
blnSearchComp = True
End Sub
Sub TestAdvancedSearchComplete()
Dim sch As Outlook.Search
Dim rsts As Outlook.Results
Dim i As Integer
blnSearchComp = False
Const strF As String = "urn:schemas:httpmail:subject LIKE
'%mykeyword%'"
Const strS As String = "Inbox"
Set sch = Application.AdvancedSearch(strS, strF)
While blnSearchComp = False
DoEvents
Wend
Set rsts = sch.Results
For i = 1 To rsts.Count
MsgBox rsts.Item(i).SenderName
Next
End Sub

I'd like to tell you how that went but it ran for an hour at about full
CPU and I had to kill OL. I guess I want to ask, how would you
distinguish the 3 result sets in this case? And could I concatenate
them?

Now, if I uncommented the 2 lines and ran all 3 Test... calls then
clearly I could distinguish the 3 result sets, so perhaps I might build
a listbox from the result items. But I'd far prefer to have an OL folder
interface. But since Sue says you can't append to a Search result
folder, do I have any options for a multi-folder result set? (Maybe I
should also ask if scope can be built from several folders, when the
search filter is to be identical for each.)
 
K

Ken Slovak - [MVP - Outlook]

To search in multiple folders in one search you can separate the list of
folders with commas, I find it works best in those cases if I surround each
folder name with single quotes whether or not they have spaces in the names.
All the searched folders must be in the same mail store (PST or mailbox),
you can't search across stores.

To be able to identify various searches when they complete and fire the
AdvancedSearchComplete event you add unique Tag properties to each search
you start, then check the Tag of the completed search when it's passed to
you in that event handler.

You can aggregate the searches in any way you want: a listbox, grid control,
disconnected recordset, a collection, the list is limited by your
imagination. I often use a grid control when I want to aggregate results
from multiple folders into one visual interface.
 
W

Wild Bill

Awesome - helluva thorough answer! Lots to work with. It brings to
mind, "I just hope you're not busy for about a month" - Tommy Chong,
1978 :) :)
 

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