title: How to create word docs programmatically from a template
body: <p>I am trying to create about 600 reports in Microsoft office Word. The documents are populated with data from a database, and images found on a local drive.
  I have figured out, that I might create a Word Template project in visual studio 2010, and program the template, so that when you enter a single value (id-number), it automatically fills out the entire document.</p>
  <p>I am quite confident that this is possible. the only problem is. How do I loop through all entries in the database, open a new document based on the template and set the id-value?</p>
  <pre><code>for(int i = 0; i &lt; idnumbers.Count(); i++)
  {
       Word.Application app = new Word.Application();
       Word.Document doc = app.Documents.Add(@&quot;C:\..\WordGenerator\bin\Debug\WordTemplate.dotx&quot;);
       //input the id-number below: HOW??
  
       doc.SaveAs(FileName: @&quot;c:\temp\test.docx&quot;); 
  }
  </code></pre>
  <p>The application is supposed to run only once, generating the reports, and it doesn´t have to be fast. It just has to be easy to develop.</p>
  <p>The problem here is, that it seems that the DocumentBase object is not accessible outside the Word project. The substitute <code>Microsoft.Office.Interop.Word.Document</code> does not have functionality like <code>SelectContentControlsByTitle</code> that allows me to find and set my <code>ContentControls</code>. And that is exactly what I need to do..</p>
  <hr />
  <p>EDIT: This is what my code looks like now to insert the text into my field:</p>
  <pre><code>Word.Application app = new Word.Application();
  
  Word.Document doc = app.Documents.Add(@&quot;C:\..\test.dotx&quot;);
  
  foreach (Word.ContentControl cc in doc.SelectContentControlsByTitle(&quot;MyCCTitle&quot;))
  {
      cc.Range.Text += &quot;1234&quot;;
  }
  
  doc.SaveAs(FileName: @&quot;c:\temp\test.docx&quot;);
  </code></pre>
  <p>Then an eventhandler on my template on BeforeSave fills out the document based on the text in MyCCTitle-titled object.</p>