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 catalog merge area", _ Title:="Incorrect Shape Type" End If End Sub
Now, we're ready to perform the catalog merge. The next code procedure executes the merge to a new publication, then assigns several tags to each page in the publication. These tags contain the following information:
- The file path and name of the data source used
- The table in the data source used
- The number of records included in the merge
- The publication that contains the catalog merge area used to generate the merge
- The data and time Publisher performed the merge
The code then calls two other procedures, which we'll examine in turn.
Sub TagMergePagesAndShapes() Dim d As Document Dim p As Page Set d = ActiveDocument.MailMerge.Execute(msoFalse, pbMergeToNewPublication) 'Add tags containing merge information 'to each page For Each p In d.Pages With p.Tags .Add "Data Source", ActiveDocument.MailMerge.DataSource.Name .Add "Data Source Table", ActiveDocument.MailMerge.DataSource.TableName .Add "Number of Records", ActiveDocument.MailMerge.DataSource.RecordCount .Add "Merge Pub", ActiveDocument.FullName .Add "Page Creation Date", CStr(Now) End With Next p 'Call sub to number tag shapes by record NumberRecordTags d, ActiveDocument 'Call sub to add values to the numbered tags AddValuesToTags d, ActiveDocument End Sub
The first of these functions numbers shapes containing tags sequentially throughout the publication. The code takes advantage of two facts:
- Publisher indexes Shape objects in the Shapes collection according to the order in which they were added to the page.
- When performing a merge, Publisher adds all the shapes for the first merge record, then all the shapes for the second, and so on throughout sequentially numbered pages.
Because of this, we can assume that the first shape in a publication with a tag named Item contains the Item data field value for the first record, and so on.
The code uses the FindShapesByTag method to identify all the shapes that contain a tag named for a specific data field (for example, Item). The code iter