WriteDataToBM oThisDoc, "Name", oSourceDoc.FormFields("Name").Result You of course could have many similar files to choose from. In this case the user only has one option (i.e., JSRagman.doc). Using this dialog, the user selects the data source for the document. When AutoNew executes, it call a procedure "GetOpenFileName" which presents a dialog to the user.
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page. Sub WriteDataToBM(ByRef oDoc As Word.Document, bmName As String, strText As String) GetOpenFileName = WordBasic.FileNameInfo$(.Name, 1) WriteDataToBM oThisDoc, "SpouseName", Left(oTbl.Cell(2, 4).Range.Text, Len(oTbl.Cell(2, 4).Range.Text) - 2) WriteDataToBM oThisDoc, "FirstName", Left(oTbl.Cell(2, 3).Range.Text, Len(oTbl.Cell(2, 3).Range.Text) - 2) WriteDataToBM oThisDoc, "Address", Left(oTbl.Cell(2, 2).Range.Text, Len(oTbl.Cell(2, 2).Range.Text) - 2) WriteDataToBM oThisDoc, "Name", Left(oTbl.Cell(2, 1).Range.Text, Len(oTbl.Cell(2, 1).Range.Text) - 2) 'The "Left" method is used to strip the end of cell marker from the cell text. 'Insert the text content of the appropriate source document table cell in the bookmarks Set oSourceDoc = Documents.Open(FileName:=strTemp, Visible:=False) 'Open the source file "invisibly to the user" to extract data 'Executes each time a new document is created from the template. Each one is similar, but unique to the specific person, client, product etc., and each one has a unique file name. You may have one or a thousand such files. The illustration below depicts a typical source document file with a table containing client data. Again, my objective is to demonstrate the methods and not a specific application. I understand that my examples may appear unrealistic or silly. I will then show you how to place that data in a new document bookmark, protected form field and content control.
In the examples that follow, I will show you how to extract data stored in a source document table, protected document form field, and a content control.
Note: The general concept presented in this page was applied to my DocBundler Word template add-in I will leave the specific application of these methods up to you, but one application that I have often been asked about is how to a create master file of data about a certain person, client, or product, etc., and use that file as a source of data for creating one or more specific documents related to that person, client or product. Using a source file as a data sheet in this manner you can build a collection of templates that draw or extract data from a common source data file.
The data in the source file can be tabular, contained in the form fields of a protected form, or if you are using Word 2007 or later, contained in content controls. This Microsoft Word Tips & Microsoft Word Help page is is a companion to my Extract Batch Form Data page and shows you how to extract data from a targeted source file.