d If Next s Next d End Sub
Now, let's examine a few more complex examples that use the Publisher catalog merge functionality.
By design, when you perform a catalog merge, the publication pages you create contain no information about the catalog merge publication or data source used to create them. This is true whether you create a new publication, or append the catalog merge results to an existing publication. The publication has no "memory" of being created by the catalog merge process at all. And there are several good reasons, including security issues, why Publisher does not store information about the publication or data source used to perform the merge.
But what if there are instances where you want to persist this information in the catalog publication? The next two code examples use tags to store this information; the first scenario involves merging repeatedly to an existing publication, while the second involves creating a catalog publication.
Note The following examples assume you are familiar with the catalog merge functionality in Publisher 2003. For more information on programmatically executing catalog merges, see Creating Catalogs with Microsoft Office Publisher 2003.
Scenario: Using Publisher Tags to Track Multiple Catalog Merges into a Single Document
Suppose you want to build a single publication as a catalog, and you use several different catalog merge areas and/or data sources. Perhaps each section of your catalog uses a different data source, or each section has a different layout and so uses a different catalog merge area. Because of the complexity of the merge, you want to know which pages in the publication where created with which data sources and publications, and when they were created.
The following code example shows how you could use page-level tags to store this information.
First, the example opens the specified publication and records how many pages are in the publication prior to executing the merge. Then the code executes the merge, appending the resulting pages to the specified publication. Next, the code adds tags to each new page created by the merge. These tags contain the following information:
- The data source used in the merge
- The publication that contains the catalog merge area used in the merge
- Date and time of the merge
The code then creates or increments a document-level tag called "Number Of Merges," which contains the number of merge results that are appended to the specified publication. Finally, the code creates another document-level tag to contain the number of records processed in the current catalog merge.
This example assumes you have created a catalog merge, and written a procedure that calls this function.
Note This example stores information, including the file path and name of the publication and data source used to perform the merge, in the specified publication. Depending on your business processes, storing this information in another publication may present a security concern.
Function ExecuteMergeAndTagPages(filePath As String) Dim d As Document Dim p As Page Dim intPages As Integer Dim i As Integer Dim t As Tag Dim TagExists As Boolean TagExists = False 'Open the publication 'and record current number of pages Set d = Application.Open(filePath) intPages = d.Pages.Count 'Close publication without saving d.Application.Quit 'Append the merge to the specified publication Set d = ActiveDocument.MailMerge.Execute(Pause:=False, _ Destination:=pbMergeToExistingPublication, _ Filename:=filePath) 'Add page-specific tags 'containing merge data information 'to each new page created by the merge For i = (intPages + 1) To d.Pages.Count Set p = d.Pages(i) With p.Tags .Add "Data Source", ActiveDocument.MailMerge.DataSource.Name .Add "Merge Pub", ActiveDocument.FullName .Add "Page Creation Date", CStr(Now) End With Next i 'Find the Number of Merges tag 'if it already exists 'and increment it for this merge For Each t In d.Tags If t.Name = "Number of Merges" Then t.Value = t.Value + 1 TagExists = True Exit For End If Next t 'Create the Number of Merges tag 'if it does no