Nico
Nico

Reputation: 76

VBA Outlook - read the current explorer search filter

I want to read the current filter of the Explorer.Search function.
I tried it with this code:

private sub read_old_filter()
    dim objExplorer as Outlook.Explorer
    dim oldFiler as String

    set objExplorer = Outlook.ActiveExplorer
    oldFilter = objExplorer.search
end sub

But objExplorer.search is a function, so it cant work.

I want to reuse the old filter. I have makros how filter for something like strFilter = "received:(today OR yesterday)". And the private sub read_old_filter() is in my userform. I want to connect the old filter and the new filter of the userform.

Can anyone help me?
Thanks for any commend and answer. Kind reguards, Nico

Upvotes: 2

Views: 1265

Answers (2)

Nico
Nico

Reputation: 76

I got a great Idea. I make in my Modul1 global values. In this values are the filter options of the macros. I save with an Integer the current filter number. A Public Function read the Integer and with Select Case it pass the right filter to the userform.

Modul1

Const FILTER_1 As String = ""                               'löscht alle filter
Const FILTER_2 As String = "followupflag:(followup flag)"   'zeigt nur Tasks an
Const FILTER_3 As String = "received:(today OR yesterday)"  'zeigt nur mails von heute und gestern
Dim filterUsed As Integer

'select right filter and save the number
Sub ViewFilter_all()
    filterUsed = 1
    Call filter_aktualisieren(FILTER_1)
End Sub
Sub onlyTasks()
    filterUsed = 2
    Call filter_aktualisieren(FILTER_2)
End Sub
Sub ViewFilter_today()
    filterUsed = 3
    Call filter_aktualisieren(FILTER_3)
End Sub
'search function for the macros
Private Sub filter_aktualisieren(strFilter As String)
    Dim objExplorer As Object
    Set objExplorer = Outlook.ActiveExplorer

    objExplorer.search strFilter, Outlook.OlSearchScope.olSearchScopeCurrentFolder
    objExplorer.Display
End Sub
'function to pass the filter to the userform
Public Function getFilterUsed() As String
    Select Case filterUsed
        Case 1
            getFilterUsed = FILTER_1
        Case 2
            getFilterUsed = FILTER_2
        Case 3
            getFilterUsed = FILTER_3
        Case Else
            getFilterUsed = "none"
    End Select
End Function

Part of Userform

Public Sub UserForm_Initialize()
    'code....
    'vorFilter is a Global String in my Userform
    vorFilter = getFilterUsed()
    'code....
End Sub

Hope the Solution help other People too.

Upvotes: 0

LocEngineer
LocEngineer

Reputation: 2917

This is obviously not directly possible in Outlook VBA. Although there is a ActiveExplorer.CurrentView.Filter as well as an XML property of the View, this will not expose the current filter query/condition.

However upon searching I came across this thread which is mentioning Redemption which seems to provide what you need: How to get a search folder criteria in Outlook

Hope this helps.

Upvotes: 3

Related Questions