 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 catalog publication if we want to make a change. For example, suppose we later decide to offer a ten percent discount on any item priced over $10. We could add an additional column for the discount price to the data source, and then regenerate the catalog. However, we may not have permission to alter the data source, and we may have further customized the catalog with changes that are lost if we regenerate it. Because of the information we store in the tags, we can accomplish the same thing without either altering the source files or losing our custom changes.
The following code finds all the text boxes that contain a Price tag. If the Price Tag object Value property is greater than 10, the code calculates a discounted price and inserts text listing that price into the shape.
Function DisplayDiscount(d As Document) Dim sr As ShapeRange Dim sh As Shape Dim dis As Single Dim txt As TextRange With d 'Find all shapes with a Price tag Set sr = .FindShapesByTag("Price") If Not sr Is Nothing Then For Each sh In sr 'If the Price is greater than ten dollars If sh.Tags("Price").Value > 10 Then 'Create a discount price dis = (sh.Tags("Price").Value) - ((sh.Tags("Price").Value) * 0.1) 'Add text to the shape 'that lists the discount price Set txt = sh.TextFrame.TextRange.InsertAfter _ (NewText:=vbLf & "Now only " & CStr(Round(dis, 2)) & "! Save 10%") 'Format discount price text With txt .Font.Color.RGB = 255 .Font.Bold = msoTrue End With End If Next sh End If End With End Function
Below is a diagram of the Publisher object model section pertaining to the Tags collection.
Figure 3. The Tags object model section of Publisher
Hopefully, you can see just how flexible and useful tags can be to track programmatic metadata. Whether it's attribute information that isn't already tracked in any available object model properties, or variable data that is needed for running some procedure or programmatic functionality, tags are an ideal storage mechanism for persisting the data you need for your business processes within your publications the