Add Filters to a Publisher Mail Merge Using Excel VBA
Question details
Add multiple conditions to a Publisher mail merge controlled by Excel VBA.

- Product
- Microsoft Excel VBA and Microsoft Publisher
- Device & OS
- not provided
- Scenario
- An Excel VBA macro controls a Publisher mail merge using an Excel data source.
- Observed behavior
- The user wants to merge only records that meet the specified conditions.
Confirm that the Publisher publication is open and already connected to the intended Excel data source, and check its column headers. In the Excel VBA editor, use Tools > References to select the Microsoft Publisher and Microsoft Office object libraries if they are not already available.
Apply AND filters to the Publisher mail merge data source
Use the data source's Filters collection to select matching records before running the merge.
The example assumes pubDoc is an existing Publisher.Document object for the open publication and that its mail merge data source is already connected.
In Excel, press Alt+F11 and open the procedure that controls the Publisher publication. Locate the point after the data source is connected and before the merge is executed.
Add the following code, replacing Column1, Column2, Name, and yes with the actual data source headers and values: Dim mrgMain As Publisher.MailMerge Set mrgMain = pubDoc.MailMerge With mrgMain.DataSource .Filters.Add Column:="Column1", Comparison:=msoFilterComparisonEqual, Conjunction:=msoFilterConjunctionAnd, bstrCompareTo:="Name" .Filters.Add Column:="Column2", Comparison:=msoFilterComparisonNotEqual, Conjunction:=msoFilterConjunctionAnd, bstrCompareTo:="yes" .ApplyFilter .FirstRecord = pbDefaultFirstRecord .LastRecord = pbDefaultLastRecord End With mrgMain.Execute False, pbMergeToNewPublication
Run the procedure from the VBA editor and inspect the new Publisher publication. The example includes records where Column1 equals Name and Column2 does not equal yes.

Prepare Mail Merge Data in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface for working with spreadsheets and documents. Its Microsoft Office format compatibility helps you move existing files over easily. It does not fix Publisher's VBA object model or run this Publisher-specific merge code.

Frequently Asked Questions
Where should I add the filter code in my macro?
In the Excel VBA editor, add it after the Publisher publication is open and its data source is connected, but before setting the record range and calling mrgMain.Execute.
How do the two filter conditions work together?
The example uses msoFilterConjunctionAnd, so a record must satisfy both comparisons: Column1 must equal the specified value, and Column2 must not equal its specified value.
Why is my filter not finding the expected records?
Check the connected Excel data source's headers and values against the Column and bstrCompareTo arguments in each Filters.Add call. Also confirm that the publication is connected to the intended data source.
Can WPS Office run this Publisher VBA merge?
No. This code uses Microsoft Publisher's mail merge object model. WPS Office can help you edit compatible Excel spreadsheets, but it does not fix or replace this Publisher-specific VBA automation.




