Advanced search complete event not firing in VBA

2019-09-14 00:49发布

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

2条回答
狗以群分
2楼-- · 2019-09-14 00:54

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)
查看更多
Juvenile、少年°
3楼-- · 2019-09-14 01:05

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

Dim WithEvents myOlApp As New Outlook.Application
查看更多
登录 后发表回答