VBA Error for running outlook event from excel

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
So I have some code that basically does an advance search and checks for the search to be complete and then prints it to the immediate window. Its basically an example modified from microsofts example for using advancesearch function. The error I am tripping is when i set my class application object equal to the one i use in my code:

Set OutlookEventClass.oOutlookApp = OutApp
438 : Object doesn't support this property or method

module code:
Code:
'Outlook Variables and Class
Public OutlookEventClass As New OutlookListener


Public Sub SearchOutlook()
    'Create Email
    'Generate Outlook Email for L&E
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim SearchFolder As Outlook.MAPIFolder
    Dim QuitNewOutlook As Boolean
    Dim Session As Outlook.Namespace
    Dim ExchangeStatus  As OlExchangeConnectionMode
    Dim Scope As String
    Dim Filter As String
    Dim MySearch As Outlook.Search
    Dim MyTable As Outlook.Table
    Dim nextRow As Outlook.row
    m_SearchComplete = False
    
    On Error Resume Next
    Set OutApp = GetObject(, "Outlook.Application")
    
    On Error GoTo 0
  
    If OutApp Is Nothing Then
        Set OutApp = CreateObject("Outlook.Application")
        QuitNewOutlook = True
    End If
    
    Set Session = OutApp.GetNamespace("MAPI")
    Session.Logon
    
    Set OutlookEventClass.oOutlookApp = OutApp
    On Error GoTo OutlookErrors
    
    'We need to ensure outlook is fully connected
    ExchangeStatus = Session.ExchangeConnectionMode
    If ExchangeStatus <> 700 Then GoTo OutlookErrors
    
    'set scope
    Scope = "'" & Application.Session.GetDefaultFolder( _
    olFolderInbox).FolderPath _
    & "','" & Application.Session.GetDefaultFolder( _
    olFolderSentMail).FolderPath & "'"
    
    'Establish filter
    If OutApp.Session.DefaultStore.IsInstantSearchEnabled Then
        Filter = Chr(34) & "urn:schemas:httpmail:subject" _
        & Chr(34) & " ci_phrasematch 'Office'"
    Else
        Filter = Chr(34) & "urn:schemas:httpmail:subject" _
        & Chr(34) & " like '%Office%'"
    End If
    Set MySearch = OutApp.AdvancedSearch( _
    Scope, Filter, True, "MySearch")
    
    While m_SearchComplete <> True
        DoEvents
    Wend
    
    'Establish filter
    If OutApp.Session.DefaultStore.IsInstantSearchEnabled Then
        Filter = Chr(34) & "urn:schemas:httpmail:subject" _
        & Chr(34) & " ci_phrasematch 'Virginia'"
    Else
        Filter = Chr(34) & "urn:schemas:httpmail:subject" _
        & Chr(34) & " like '%Virginia%'"
    End If
    
    Set MySearch = OutApp.AdvancedSearch( _
    Scope, Filter, True, "MySearch")


    Set MyTable = MySearch.GetTable
    Do Until MyTable.EndOfTable
        Set nextRow = MyTable.GetNextRow()
        Debug.Print nextRow("Subject")
    Loop


    If QuitNewOutlook Then
        OutApp.Quit
    End If
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    'Set ExchangeStatus = Nothing Possible Memory Leak?
    
    Exit Sub


OutlookErrors:


            Debug.Print Err.Number & " : " & Err.Description
            Set OutMail = Nothing
            'Set ExchangeStatus = Nothing Possible Memory Leak?
            If Not OutApp Is Nothing And QuitNewOutlook Then
                OutApp.Quit
            End If
            Set OutApp = Nothing
            'QueryRunning = False
    


End Sub

class code:
Code:
Option Explicit


Public WithEvents oOutlookApp As Outlook.Application


Public Sub oOutlookApp_AdvancedSearchComplete(ByVal SearchObject As Search)
    If SearchObject.Tag = "MySearch" Then
        m_SearchComplete = True
    End If
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
When you type the dot after OutlookEventClass, do you see oOutlookApp listed in the dropdown?
 
Upvote 0
Is that the sum total of the code in the class module?
 
Upvote 0
Is that the sum total of the code in the class module?

There is additional code in the class module but it is for a send event has been functioning properly for a while. It includes the following:

Code:
Option Explicit


Public WithEvents oMailItem As Outlook.MailItem
Public WithEvents oOutlookApp As Outlook.Application


Public Sub oMailItem_Send(Cancel As Boolean)
'some code for if the send event failed we write to a database
End Sub
 
Upvote 0
And is that the sum total? There's no Class_Initialize for example?
 
Upvote 0
And is that the sum total? There's no Class_Initialize for example?

Sorry , still a bit confused what sum total is but that is it regarding the class sub routines and events i catch. I declare it in the code above using:

'Outlook Variables and Class
Public OutlookEventClass As New OutlookListener

Other than that I do not reference the class anywhere else except for that one send event.
 
Last edited:
Upvote 0
I just meant is that all of the code.
 
Upvote 0
I just meant is that all of the code.

I found the error causing the issue. For setting the scope i use application instead of outapp to set the folder path. Everything is working now. I did need to add a do loop though to pick up the event. I must have left that out also.

Thanks for your help!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top