AdvancedSearchComplete Even Not Firing

D

Dav Banks

Hi,
I'm having a bit of a problem getting the AdvancedSearchComplete to fire.
I'm sure my search is working - I added two message boxes after the search,
one so I could introduce a delay to allow the search to complete and a
second to check the count of the result. The count comes up good.
However, when I just let the function end and wait for the event to fire,
I get nothing. I'm sure it's something simple but I can't see it! Any ideas?


<--- Code --->
Public objSrch As Search

Public Sub Archive(fldSource As MAPIFolder, fldDestination As MAPIFolder,
date1 As Date, Optional recursive As Boolean = False)
Dim fldNewDest As MAPIFolder
Dim fldNewSource As MAPIFolder
Dim fldMail As MailItem
Dim i As Integer
Dim scope As String
Static cnt

Const srch As String = """urn:schemas:httpmail:datereceived"" <=
'1/1/2005'"
scope = "SCOPE ('shallow traversal of """ & fldSource.FolderPath &
"""')"
Set objSrch = Application.AdvancedSearch(scope, srch, False, "Test")
'Test Messages
'MsgBox "Filter = " & objSrch.Filter & Chr(13) _
& "Folder = " & objSrch.scope
'MsgBox objSrch.Results.Count
Set objSrch = Nothing
End Sub

Public Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
Dim rslt As Outlook.Results
Set rslt = SearchObject.Results
MsgBox "Test1"
MsgBox SearchObject.Tag '& " Complete with " & rslt.Count & " results"
End Sub
<--- Code --->

db
 
M

Michael Bauer

Am Wed, 7 Jun 2006 14:21:21 -0400 schrieb Dav Banks:
Set objSrch = Nothing

Please move that line into the SearchComplete event. (That sample you could
also do without the variable.)
 
D

Dav Banks

Thanks for the feedback!

Moving the line did not help. Nor did removing it.

The event doesn't seemed to be called at all, i.e., the 'Test1' message is
never displayed. I was thinking it had something to do with how it was
declared or maybe not being attached to the application object somehow. I've
tried declaring it as Public and Private - I've seen it done both ways. But
neither worked.

db
 
K

Ken Slovak - [MVP - Outlook]

Where is your search complete procedure running? Is it in the
ThisOutlookSession class or another class module or do you have it in a code
module where the callback wouldn't fire?
 
D

Dav Banks

So...

I got the event to fire by moving the event code to the 'ThisOutlookSession'
module but the while...DoEvents loop seems to not respond now. I put a
message box in the event code to test firing and the while condition - both
were good - but the while loop seems never to end - the code after it is
never executed.

<-- ThisOutlookSession -->
Public Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
Set rslt = SearchObject.Results
'MsgBox SearchObject.Tag & " Complete with " & rslt.Count & " results"
'MsgBox rslt Is Nothing
blnSearchComp = True
'Set objSrch = Nothing
End Sub

<-- Form Code -->
Public Sub Archive(fldSource As MAPIFolder, fldDestination As MAPIFolder,
date1 As Date, Optional recursive As Boolean = False)
Dim fldNewDest As MAPIFolder
Dim fldNewSource As MAPIFolder
Dim fldMail As MailItem
Dim scope As String

Const srch As String = """urn:schemas:httpmail:datereceived"" <=
'1/1/2005'"
scope = "SCOPE ('shallow traversal of """ & fldSource.FolderPath &
"""')"
Set objSrch = Application.AdvancedSearch(scope, srch, False, "Test")
'MsgBox "Filter = " & objSrch.Filter & Chr(13) _
& "Folder = " & objSrch.scope
'MsgBox objSrch.Results.Count
blnSearchComp = False
Set rslt = Nothing
'While blnSearchComp = False
While rslt Is Nothing
DoEvents
Wend
MsgBox "Hello"
MsgBox rslt.Count
End Sub


db
 
K

Ken Slovak - [MVP - Outlook]

I don't see why you need a Do Loop. Just have the complete event do whatever
you want. Or set up a global Boolean in a code module and set it False on
starting the search and True in the complete event.

Where is that result object declared? Is it declared with global scope?
 
D

Dav Banks

I was using the examples from this group that other people seemed to have
success with. I initially used a global Boolean (blnSearchComp in the code)
but it had the same results - the while loop never saw the change in value
after the complete event fired.

I've had the result object declared public at the global level of both the
code module and the ThisOutlookSession module with the same results.

I wanted to go this way because I eventually want the Archive routine to be
recursive but I didn't want it to bog down the system by calling many
searches before the previous one finished. This way I could do one at a
time.

Is there any reason why the while loop isn't seeing the variable value
change?

db
 
D

Dav Banks

What worked...

Declaring the results object and the Boolean as global in the form code.
Referring to the objects with the form name from the ThisOutlookSession...
Archive.blnSearchComp = True
Set Archive.rslt = SerchObject.Results

db
 
K

Ken Slovak - [MVP - Outlook]

Ah, then they weren't true globals. Those would have been declared as Public
in a code module, not a form or class. In a form or class you always would
need to qualify your reference with the class or form object.
 

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