title: Programmatically printing different types word document in C#
body: <p>I would like to ask how can I print a variety of documents from C #. I did this by modifying the document word by replacing the text for example:</p>
  
  <pre><code>&lt;Value&gt; -&gt; value. 
  </code></pre>
  
  <p>I have various documents, ie. a plain text document and the document to the table. The table shows the installments to repay. Document to the table is a problem because I do not know how to retrieve data and how to display them.</p>
  
  <p>The whole still must be possible to configure, add, edit, delete documents.
  I have somethink as You can see below.</p>
  
  <pre><code>public string CreateDoc(object fileName, string _query, string _numUmowy)
      {
          object oMissing = System.Reflection.Missing.Value;
          object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
  
          string message = string.Empty;
  
          Application wordApp = new Application();
          Document aDoc = null;
  
          if (File.Exists((string)fileName))
          {
              DateTime today = DateTime.Now;
  
              object readOnly = false;
              object isVisible = false;
  
              wordApp.Visible = false;
  
              aDoc = wordApp.Documents.Open(ref fileName, ref oMissing,
                  ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                  ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                  ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                  ref oMissing, ref oMissing);
  
              aDoc.Activate();
  
  
  
              using (SQLiteConnection conn = new SQLiteConnection(GetConnStr()))
              {
                  using (SQLiteCommand cmd = new SQLiteCommand(_query, conn))
                  {
                      conn.Open();
  
                      cmd.Parameters.AddWithValue("@UmowaNum", _numUmowy);
  
                      using (SQLiteDataReader rdr = cmd.ExecuteReader())
                      {
                          if (rdr.HasRows)
                          {
                              rdr.Read();
  
                              int rdrLength = rdr.FieldCount;
                              for (int i = 0; i &lt; rdrLength; i++)
                              {
                                  this.FindAndReplace(wordApp, "&lt;" + rdr.GetName(i) + "&gt;", rdr[i].ToString());
                              }
  
  
                              aDoc.PrintOut(oMissing, oMissing, oMissing, oMissing,
                                  oMissing, oMissing, oMissing, oMissing, oMissing,
                                  oMissing, oMissing, oMissing, oMissing, oMissing,
                                  oMissing, oMissing, oMissing, oMissing);
  
                              message = "Drukowanie zakończone powodzeniem!";
                          }
                      }
                  }
              }
  
              aDoc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
              wordApp.Quit();
  
              return message;
  
          }
          else
          {
              message = "Plik nie istnieje!";
              return message;
          }
      }
  </code></pre>
  
  <p>This code print Agreement, but I don't know how to print Installment.
  Agreement.doc</p>
  
  <pre><code>aa bbb fbdsf
  &lt;Value1&gt;
  aa bbb fbdsf
  &lt;Value2&gt;
  aa bbb fbdsf
  &lt;Value3&gt;
  aa bbb fbdsf
  &lt;Value4&gt;
  aa bbb fbdsf
  &lt;Value5&gt;
  </code></pre>
  
  <p>Installment.doc</p>
  
  <pre><code>aa bbb fbdsf
  &lt;Value1&gt;
  aa bbb fbdsf
  &lt;Value2&gt;
  aa bbb fbdsf
  TABLE (start)
  1. &lt;val1.1&gt; &lt;val1.2&gt; &lt;val1.3&gt;
  2. &lt;val2.1&gt; &lt;val2.2&gt; &lt;val2.3&gt;
  3. &lt;val3.1&gt; &lt;val3.2&gt; &lt;val3.3&gt;
  4. &lt;val4.1&gt; &lt;val4.2&gt; &lt;val4.3&gt;
  5. &lt;val5.1&gt; &lt;val5.2&gt; &lt;val5.3&gt;
  TABLE (end)
  </code></pre>