title: Generate Word Documents (in Excel VBA) from a series of Document Templates
body: <p>Hey all. I'll try to make this brief and simple. :)</p>
  
  <p><strong>I have</strong></p>
  
  <ol>
  <li>40 or so boilerplate word documents with a series of fields (Name, address, etc) that need to be filled in. This is historically done manually, but it's repetitive and cumbersome.</li>
  <li>A workbook where a user has filled a huge set of information about an individual.</li>
  </ol>
  
  <p><strong>I need</strong></p>
  
  <ul>
  <li>A way to programatically (from Excel VBA) open up these boilerplate documents, edit in the value of fields from various named ranges in the workbook, and save the filled in templates to a local folder.</li>
  </ul>
  
  <p>If I were using VBA to programatically edit particular values in a set of spreadsheets, I would edit all those spreadsheets to contain a set of named ranges which could be used during the auto-fill process, but I'm not aware of any 'named field' feature in a Word document.</p>
  
  <p>How could I edit the documents, and create a VBA routine, so that I can open each document, look for a set of fields which might need to be filled in, and substitute a value?</p>
  
  <p>For instance, something that works like:</p>
  
  <pre><code>for each document in set_of_templates
      if document.FieldExists("Name") then document.Field("Name").value = strName
      if document.FieldExists("Address") then document.Field("Name").value = strAddress
      ...
  
      document.saveAs( thisWorkbook.Path &amp; "\GeneratedDocs\ " &amp; document.Name )
  next document
  </code></pre>
  
  <p>Things I've considered:</p>
  
  <ul>
  <li>Mail merge - but this is insufficient because it requires opening each document manually and structuring the workbook as a data source, I kind of want the opposite. The templates are the data source and the workbook is iterating through them. Also, mail merge is for creating many identical documents using a table of different data. I have many documents all using the same data.</li>
  <li>Using placeholder text such as "#NAME#" and opening each document for a search and replace. This is the solution I would resort to if nothing more elegant is proposed.</li>
  </ul>