Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

excel - Advanced search complete event not firing in VBA

As the title suggests I cannot get the advanced search complete event to fire. I am running VBA through Excel 2013. I have a sub running in worksheet1 which creates a test object which then runs an advanced search. This is all working fine, and the search object does return results. However, the advanced search complete event never fires. Any ideas?

Thanks!

Main code:

Sub testing()

Dim test As Class1
Set test = New Class1

Call test.TestAdvancedSearchComplete

End Sub

Class 1:

Dim myOlApp As New Outlook.Application
Public blnSearchComp As Boolean
Dim sch As Outlook.search
Dim rsts As Outlook.Results



Private Sub myOlApp_AdvancedSearchComplete(ByVal SearchObject As search)
    MsgBox "The AdvancedSearchComplete Event fired."
    blnSearchComp = True
End Sub



Sub TestAdvancedSearchComplete()
Dim i As Integer

blnSearchComp = False
Const strF As String = "urn:schemas:mailheader:subject = 'Test'"
Const strS As String = "Inbox"

Set sch = myOlApp.AdvancedSearch(strS, strF, False, “Test”)

While blnSearchComp = False
    DoEvents
Wend

Set rsts = sch.Results
For i = 1 To rsts.Count
    Debug.Print rsts.Item(i).SenderName
Next
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To allow raising events you need to declare your objects with WithEvents

Dim WithEvents myOlApp As New Outlook.Application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...