ApplyFilter Method

Microsoft Office Object Model

ApplyFilter Method


Applies a filter to a mail merge data source to filter specified records meeting specified criteria.


expression   Required. An expression that returns one of the objects in the Applies To list.


This example adds a new filter that removes all records with a blank Region field and then applies the filter to the active publication.

Sub OfficeFilters()
    Dim appOffice As OfficeDataSourceObject
    Dim appFilters As ODSOFilters

    Set appOffice = Application.OfficeDataSourceObject

    appOffice.Open bstrConnect:="DRIVER=SQL Server;SERVER=ServerName;" & _
        "UID=user;PWD=;DATABASE=Northwind", bstrTable:="Employees"

    Set appFilters = appOffice.Filters

    MsgBox appOffice.RowCount

    appFilters.Add Column:="Region", Comparison:=msoFilterComparisonEqual, _
        Conjunction:=msoFilterConjunctionAnd, bstrCompareTo:="WA"

    MsgBox appOffice.RowCount

End Sub