问题
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
回答1:
To allow raising events you need to declare your objects with WithEvents
Dim WithEvents myOlApp As New Outlook.Application
回答2:
The AdvancedSearchComplete event has a special procedure name which the compiler needs to see in order to tie your code to the event. Change the procedure name to:
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
来源:https://stackoverflow.com/questions/31909315/advanced-search-complete-event-not-firing-in-vba