In my last entry, I talked about the Tags collection, which you can use as generic holding bins for whatever data you want to persists within your publications, pages, or shapes. Now let’s look at some actual examples.
So far, the examples I’ve been able to think up use tags for storing two general types of information:
· Publication, page, or shape attribute information that isn’t already stored in any of the available object model properties.
· Variable data that is needed for running some procedure or add-in functionality.
Example Using a Document-Level Tag
Suppose you’ve created a catalog of your products, including prices that are subject to change over time. You want to be sure none of your employees prints the catalog for a customer once it’s more than three months old. So when you’ve finished creating the catalog, you add a document tag, called Expiration Date, and set its value to a date 90 days in the future.
ActiveDocument.Tags.Add “ExpirationDate”, CStr(Now + 90)
And then you write an event handler for opening the document. The event handler compares the value of the Expiration Date tag with the current date. If the publication is opened after the expiration date, a dialog box displays advising the user not to print the catalog because the prices may not be accurate anymore.
(You’d place this event handler in the ThisDocument object module.)
Private Sub Document_Open()
If CDate(ActiveDocument.Tags(“ExpirationDate”).Value) < Now Then
MsgBox Prompt:=”Prices in this catalog may be out of date.” & _
vbLf & “Do not release this catalog to a customer” & vbLf & _
“without checking that prices are accurate.”, _
Title:=”Expiration Date Passed”
Example Using a Page-Level Tag
By design, when you perform a catalog merge, the publication you create or append with the merge results contains no information about the merge publication or data source used to create it. It has no ‘memory’ of having been created by a catalog merge.
But what if you wanted to retain that information in the target publication? Suppose you’re building a single publication as a catalog, and you’re using catalog merges from several different publication and/or data sources. (Maybe each section of your catalog uses a different data source, or each section has a different layout and so uses a different catalog merge area.) It’d be reasonable to want to know which pages in the publication where created with which data sources and publications, and when.
The following example uses page-level tags to store this information.
First, it opens the target publication and records how many pages are in the publication prior to the merge being executed. Then it executes the merge, appending the resulting pages to the target publication. Next, the code adds three tags to each new page created by the merge. These tags contain the following information:
· The data source used in the merge
· The publication that contains the catalog merge area used in the merge
· Date and time of the merge
The code even creates or increments a document-level tag called “No of Merges”, which contains the number of merge results that have been appended to the target publication.
Function ExecuteMergeAndTagPages(filePath As String)
Dim d As Document
Dim p As Page
Dim intPages As Integer
Dim i As Integer
Dim t As Tag
Dim TagExists As Boolean
TagExists = False
Set d = Application.Open(filePath)
intPages = d.Pages.Count
Set d = ActiveDocument.MailMerge.Execute(Pause:=False, _
For i = (intPages + 1) To d.Pages.Count
Set p = d.Pages(i)
.Add “Data Source”, ActiveDocument.MailMerge.DataSource.Name
.Add “Merge Pub”, ActiveDocument.FullName
.Add “Page Creation Date”, CStr(Now)
For Each t In d.Tags
If t.Name = “No of Merges” Then
t.Value = t.Value + 1
TagExists = True
If TagExists = False Then
d.Tags.Add Name:=”No of Merges”, Value:=1
Once you’ve stored this information, of course, you can write code that programmatically queries the tags and takes action based on the results. For example, you could write a procedure that deletes all the pages created using a certain data source.
Next time, I’ll see if I can’t come up with some code samples that use tags at the individual shape level.