Publisher: Using Tags to Store Custom Properties (Part 3)

First, I discussed the tags collection, which you can use as generic holding bins for whatever data you want to persists within your publications, pages, or shapes.

Next, I showed you a few examples using document and page-level tags.

So now, to round things out, let’s look at a short example using tags stored in shapes.

Let go back to the book example I mentioned in the first entry: you’ve created a book, and each chapter is a separate publication. Now, let’s further suppose that within each chapter you’re using textbox shapes for a number of different functions: the main chapter text, but also side-bar stories, pull quotes, figure captions, that kind of thing. Programmatically, there’s no way to tell the function of each textbox shape. Except you used tags to do exactly that. When you created each publication, you added a Tag object to each text box that denoted what you’re using it for. Like so:

Activedocument.Selection.ShapeRange(1).Tags.Add "StoryType", "PullQuote"

(The above code assigns the tag to the first shape in the ShapeRange you currently have selected in the active document. Obviously, if you’ve only got one shape selected, then that’s shape one.)

Now, suppose you decide later that you want to change the font you used for all the pull quotes, and bump up the point size while you’re at it. You can quickly create a procedure that identifies all the text boxes containing pull quotes, based on the value of their ‘StoryType’ tag, and makes the appropriate changes.

Sub FormatPullQuotes()

Dim d As Document

Dim p As Page

Dim s As Shape

Dim t As Tag

 

For Each d In Documents

  For Each p In d.Pages

    For Each s In p.Shapes

      If s.Type = pbTextFrame Then

        For Each t In s.Tags

          If (t.Name = "StoryType" And t.Value = "PullQuote") Then

            s.TextFrame.TextRange.Font.Name = "Arial"

            s.TextFrame.TextRange.Font.Size = 20

          End If

        Next t

      End If

    Next s

  Next p

Next d

End Sub

These examples are fairly basic, but I hope you get the idea of just how useful and flexible Tag objects can be. I’m sure, once you start looking at your own business processes, you’ll see situations where using tags will come in handy.