Publisher: Working with Multiple Open Documents
Here's something I didn't notice until I'd been programming with Publisher for awhile:
You might have noticed that in Publisher 2003, we added a Documents collection to the Application object. But, you might ask, isn't Publisher a single document interface (SDI) application? If so, shouldn't each open document have its own separate Application object?
And you'd be right on all counts. Publisher is indeed an SDI application, so each open document has its own unique Application object that you can use in code. However, the Documents collection is our way of providing you the advantages that come with a multiple document interface (MDI) in the developer environment. It gives you a simple, easy way of identifying and iterating through all the publications open on a system. This especially comes in handy when you're doing the following:
· Moving content between publications
· Performing work on several publications at a time
Here's an example of how using the Documents collection simplifies moving content between publications. The code below cuts a shape from the first page of one publication, and then pastes it onto the second page of another publication.
Sub MoveFred()
Documents("Publication1").Pages(1).Shapes("Fred").Cut
Documents("Publication2").Pages(2).Shapes.Paste
End Sub
Handling two separate Publisher application is fairly straight-forward. But things can get tricky when you have multiple instances of Publisher open, and you want to coordinate action between them. That's where the Documents collection comes in handy. For instance, suppose you've created a book, and that each chapter is a separate publication. Using the Documents collection, you can easily selected the publications you want to print, in the order you want to print them to produce a complete copy of your book.
Sub PrintMyBook()
With Documents
.Item("TitlePage").PrintOut
.Item("Chapter1").PrintOut
.Item("Chapter2").PrintOut
'etc etc
End With
End Sub
Or suppose you want to create a function that iterates through all the publication you have open, and saves copies of any Web publications as HTML into a specific folder:
Function SaveOpenPubsAsHTML(FilePath As String)
Dim d As Document
For Each d In Application.Documents
If d.PublicationType = pbTypeWeb Then
d.SaveAs Filename:=FilePath & d.Name, _
Format:=pbFileHTMLFiltered, _
AddToRecentFiles:=False
End If
Next d
End Function
(By the way, Publisher automatically adds the .htm file extension to the filename when you set the Format parameter to pbFileHTMLFiltered.)
A lot easier than having to keep track of all the individual application instances of Publisher, isn't it?