Using Building Blocks, Content Controls and XMLMappings to generatemulti-page template-generated sec

D

Dmitry

Dear Readers,

I would like some of your insight and feedback regarding a document I
am creating. Here is the scenario.

The document is a package that describes a toy collection. There is
supposed to be a section in the document which consists of a sequence
of pages that repeat an identical one-page template for each item in
the collection. I have an XML document (Excel Table mapped export)
which describes each item in the collection, and I would like to
programmatically generate these pages of the document from the content
in the XML file and the presentation in the template.

Schematically, this is what I've come up with so far:

* Create a Building Block in the main document template that consists
of the one-page template I'd like repeated in the main document. This
Building Block will have Content Controls to represent the data for an
item in the collection.

* Bind the XML data to the document as a CustomXML Part

* Count the number of items in the data store of the CustomXML Part
with an XPath query.

* Begin generating pages based the Building Block and XML data for a
single item.

* End generating these pages

I am wondering whether this is doable with the described approach. I
am relatively new to VBA but have a good handle on other scripting
languages and OO programming. So I am particularly looking for
guidance regarding any particular Objects, Classes or Properties I
should be using in each of these steps to make it all come together.
For example, I'm not entirely sure yet how the content controls within
the building block can be addressed in the code (exploring the
references and object browser/model, it's not quite clear to me). I've
been doing as much reading and experimenting as I can (see below), and
am hoping to get some feedback as a move along.

Thanks,

Dmitry




Helpful documents I've read so far:

-- Building Word 2007 Document Templates Using Content Controls
(http://msdn.microsoft.com/en-us/library/bb264571(v=office.12).aspx)

-- Creating Word 2007 Templates Programmatically (http://
msdn.microsoft.com/en-us/library/
bb266219.aspx#wd2007CreatingWord2007Templates_AboutContentControls)
 
M

Mike

Hello,

I am also new to the world of VBA and have come across a similar issue where I need to create a multi page document using a template for each page is the recordset information delivered to a bookmarked template (.dotx) file. After scouring the internet I could not find much dealing specifically with this. The simplest solution is to first create a master document file object and then create one template object instance, copy the whole page using the .wholestory method and appending to the master document. Here is some of the code: (maybe a little sloppy, I am new to this game)

Sub Append_Templates_to_Master_Document

Dim ObjMSWORD As Word.Application
Dim ObjMSDocs As Word.Documents
Dim curDoc, MasterDoc As Word.Document

Set ObjMSDocs = ObjMSWORD.Documents


ObjMSDocs.Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0

Set MasterDoc = ObjMSWORD.Documents(1)


MasterDoc.SaveAs FileName:=(Format(Date, "yyyy") & "-" & SiteID & ".docx")


For Each record in Rs

'open new instance of dotx file and link record fields to bookmarks
With ObjMSDocs
.Add Template:="E:\BB_Letter.dotx", NewTemplate _
:=False, DocumentType:=0
End With
Set curDoc = ActiveDocument

With curDoc.Bookmarks
'add fields
End With

'copy current page and append to master document
Set curDoc = ActiveDocument
curDoc.Select

Selection.WholeStory
Selection.Copy

MasterDoc.Select
Selection.EndKey wdStory, wdMove
Selection.PasteAndFormat (wdPasteDefault)
curDoc.Close
Next

end sub

One other thing: This is not completely debugged. For instance I run into errors dealing with the client no longer available if the instance is run more than once without closing Access (the filename will duplicate each time the code runs and this causes a runtime error). I just wanted to give some ideas. Let me know if you find out a better way that is more effective.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top