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
 
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!

I am curious though. By default how far back does advance search look ? I do a quick test and it only seems to pull up a recent ones. I was curious was the default was set to for length of time.

THanks
Shaun
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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