t alreay exist If TagExists = False Then d.Tags.Add Name:="Number of Merges", Value:=1 End If 'Create a tag that contains the number of records 'in this specific merge d.Tags.Add Name:="Merge " & d.Tags("Number of Merges").Value & _ " Record No", _ Value:=ActiveDocument.MailMerge.DataSource.RecordCount End Function
Once you store this information, of course, you can write code that programmatically queries the tags and takes action based on the results. You could write a procedure that deletes all the pages created using a certain data source, or created on a specified date.
Scenario: Using Publisher Tags to Store Data Record Information in Shapes
In this next scenario, we merge to a new publication, but we want to persist even more detailed information about the data source. For each shape, we want to know the record in the data source for which it contains information. We also want to know the data source fields for which it contains information, and the value of those data fields. That way, we can manipulate the shapes later based on the data source record for which they were generated, without having to refer back to or alter the data source itself, or regenerate the catalog publication.
Note Again, these examples store detailed information about the data source in the generated catalog publication. Depending on your business processes, this could present a security concern.
First, we need to add tags to the shapes in the catalog merge area that contain merge fields. When Publisher executes a catalog merge, for each record in the data source it copies each shape in the catalog merge area onto the new publication page, replacing the data fields in each shape with the actual record field data.
Figure 1. Example of a catalog merge
1. Data source with record information, such as item names and descriptions
2. Catalog merge template with catalog merge area containing merge fields that are placeholders for record information
3. Resulting merged pages displaying multiple records per page
Publisher copies the shapes in the catalog merge area, including any tags contained in those shapes. So, our first step is to add a Tag object to each shape that contains a data field. The Tag object is named the same as the data field that the shape contains. Its initial value is not important; we overwrite it later.
The following code does just that. It examines each text box shape in the catalog merge area. If the text box contains a mail merge field, the code adds a Tag object, named the same as the mail merge field, to that shape. Publisher names mail merge fields after the data source fields they represent; therefore, the code actually names the Tag object the same as the data source field. If a text box contains multiple data fields, it gets assigned multiple tags, one for each data field.
This code only applies to text data fields. There is no way in the Publisher 2003 object model to identify whether a picture shape contains a picture data field.
The code then adds another tag, named Record, to each shape in the catalog merge area. The Record tag is assigned an initial value of "0."
Sub AddTagsToMergeShapes() Dim s As Shape Dim cs As Shape Dim f As Field Dim i As Integer 'Add tags for each mail merge field in the text frame Set s = ActiveDocument.Selection.ShapeRange(1) If s.Type = pbCatalogMergeArea Then For Each cs In s.CatalogMergeItems If (cs.Type = pbTextFrame) Then If cs.TextFrame.TextRange.Fields.Count > 0 Then With cs.TextFrame.TextRange.Fields 'Fields don't support For Each...Next looping 'so create a For i = 1 to ... loop instead For i = 1 To .Count 'Identifies mail merge fields If .Item(i).Type = pbFieldMailMerge Then 'Adds tag named after mail merge field name 'minus the bracketing carrots cs.Tags.Add Mid(.Item(i).TextRange, 2, _ Len(.Item(i).TextRange) - 2), _ 0 End If Next i End With End If End If 'Add a record number tag to each shape in the merge area 'whether or not it has a mail merge field cs.Tags.Add "Record", 0 Next cs 'Error logic if shape selected is not merge area Else MsgBox prompt:="This shape is not a cat