From: Mike on 7 Apr 2010 02:52 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. --- frmsrcurl: http://msgroups.net/microsoft.public.word.vba.general/Using-Building-Blocks-Content-Controls-and-XMLMappings-to-gene
|
Pages: 1 Prev: macro keyboard shortcuts Next: Form fields vs. document properties |