ates through the resulting ShapeRange collection, sequentially numbering the value of each shape's Record tag. The first shape with an Item tag gets its Record tag value set to 1, the next shape to 2, and so on. The code does the same for tags named after each data field.
In essence, this code numbers shapes containing tags named after data fields sequentially. This enables us to determine relationships programmatically between the text boxes containing the tags. For example, the text box containing the Price tag where
Tags("Record").Value = 10 relates to the text box containing the Item tag where the Record tag value is 10 as well. We can tell which shapes contain data from which data record.
Function NumberRecordTags(d As Document, md As Document) Dim sr As ShapeRange Dim sh As Shape Dim t As Tag Dim i As Integer Dim ri As Integer With d 'For each data field in the merge data source For i = 1 To md.MailMerge.DataSource.DataFields.Count 'Find all shapes with a tag 'named the same as the specified data field Set sr = .FindShapesByTag(md.MailMerge.DataSource.DataFields.Item(i).Name) If Not sr Is Nothing Then ri = 1 For Each sh In sr For Each t In sh.Tags 'Find the corresponding Record tag 'in the shape 'and set its value If t.Name = "Record" Then t.Value = ri ri = ri + 1 Exit For End If Next t Next sh End If Next i End With End Function
We can go a step further. Now that we have the shapes containing the tags numbered sequentially, we can directly relate them to the records in the data source, which are also numbered sequentially. The following function sets the Value property of each tag named for a data field to the actual value in the corresponding data source record.
Again, the code uses the FindShapesByTag method to identify all the shapes that contain a tag named for a specific data field (for example, Item). Next, the code iterates through each record in the data source, setting each as the active record in turn.
Figure 2. Data source with selected active record
The code then finds the Shape object with the same index as the active record, and sets the tag value equal to the data field value of the active record. For example, for the first record, the code finds the first shape in the ShapeRange collection, and sets the value of its Item tag to the value of the Item field for that record. The code performs this for each record in the data source, with the end result that each Shape tag named for a data source now contains the actual value for the record.
Function AddValuesToTags(d As Document, md As Document) Dim sr As ShapeRange Dim sh As Shape Dim t As Tag Dim i As Integer Dim ri As Integer With d 'For each data field in the merge data source For i = 1 To md.MailMerge.DataSource.DataFields.Count 'Find all shapes with a tag 'named the same as the specified data field Set sr = .FindShapesByTag(md.MailMerge.DataSource.DataFields.Item(i).Name) If Not sr Is Nothing Then For ri = 1 To sr.Count 'Increment through each record in the data source md.MailMerge.DataSource.ActiveRecord = ri 'Set tag value equal to the data field value sr(ri).Tags(md.MailMerge.DataSource.DataFields.Item(i).Name).Value = _ md.MailMerge.DataSource.DataFields.Item(i).Value Next ri End If Next i End With End Function
The result is a publication that "remembers" the data source and publication used to create it. Page-level tags contain:
- The file and table name of the data source used.
- The publication containing the catalog merge area used.
- The data and time Publisher performed the merge.
While Shape-level tags contain:
- The record number of the data source record.
Note This number represents the record number among the records selected for the merge only. If you excluded records in the data source from the catalog merge, this number does not match the numbering in the original data source.
- The name of the data source field, and the field value for that particular record.
Now that this information persists within the catalog publication, we can do any number of things, without having to refer back to the data source, or regenerate the catal