title: Use Word Table Formulas in Velocity Templates
body: <p>I am using Velocity and XDocReport to generate Word (docx) documents from a template.  One thing I am doing is creating tables with data that is merged into the template, using the technique shown here:  <a href="https://code.google.com/p/xdocreport/wiki/DocxReportingJavaMainListFieldAdvancedTable" rel="nofollow">https://code.google.com/p/xdocreport/wiki/DocxReportingJavaMainListFieldAdvancedTable</a>, with before-row and after-row.  The template has a single data row, which is repeated in a foreach loop.  This all works perfectly.</p>
  
  <p>One of the columns of data in the table contains numbers.  I would like to add a second row to the table in the template which will contain a Word table formula (see <a href="http://office.microsoft.com/en-us/word-help/use-a-formula-in-a-word-table-HA102329800.aspx#_Toc281293381" rel="nofollow">http://office.microsoft.com/en-us/word-help/use-a-formula-in-a-word-table-HA102329800.aspx#_Toc281293381</a>), such as SUM(ABOVE), for the column to be summed.  My hope is that when the document is generated, this will automatically sum all of the rows of data that had been added by Velocity/XDocReport.</p>
  
  <p>Unfortunately, this does not work. There are two issues.</p>
  
  <p>(1) When I include the formula, the document is generated fully, but when I go to open the file, I get this error:  "We're sorry.  We can't open... because we found a problem.  Details.  Unexpected End of Input." It appears that when you include a format with the formula, that causes an issue and the full document does not get generated.</p>
  
  <p>When I open the working and non working documents by converting them to ZIP files and opening document.xml in a text editor, I can see that the non-working document.xml file is not a complete XML file, but gets cutoff right at the SUM(ABOVE) line:</p>
  
  <pre><code>&lt;w:instrText xml:space="preserve"&gt; =SUM(ABOVE)
  </code></pre>
  
  <p>Whereas the word file that works where I added the formula later has the full XML ending:</p>
  
  <pre><code>&lt;w:r&gt;&lt;w:instrText xml:space="preserve"&gt; =SUM(ABOVE) &lt;/w:instrText&gt;&lt;/w:r&gt;&lt;w:r&gt;&lt;w:fldChar   w:fldCharType="separate"/&gt;&lt;/w:r&gt;&lt;w:r&gt;&lt;w:rPr&gt;&lt;w:noProof/&gt;&lt;/w:rPr&gt;&lt;w:t&gt;232000&lt;/w:t&gt;&lt;/w:r&gt;&lt;w:r&gt;&lt;w:fldChar w:fldCharType="end"/&gt;&lt;/w:r&gt;&lt;w:bookmarkStart w:id="0" w:name="_GoBack"/&gt;&lt;w:bookmarkEnd w:id="0"/&gt;&lt;/w:p&gt;&lt;/w:tc&gt;&lt;/w:tr&gt;&lt;/w:tbl&gt;&lt;w:p w:rsidR="003E2115" w:rsidRDefault="003E2115"/&gt;&lt;w:sectPr w:rsidR="003E2115"&gt;&lt;w:pgSz w:w="12240" w:h="15840"/&gt;&lt;w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/&gt;&lt;w:cols w:space="720"/&gt;&lt;w:docGrid w:linePitch="360"/&gt;&lt;/w:sectPr&gt;&lt;/w:body&gt;&lt;/w:document&gt;
  </code></pre>
  
  <p>If I leave out the formula and only add it back AFTER the document has been generated, it works fine.  </p>
  
  <p>(2) When I include the table formula without any formatting, the document is generated, but the sum is always zero.  It seems like there is a timing issue involved, where the formula needs to wait until the document is generated or all of the rows have been added?</p>
  
  <p>Any idea how to fix this?  (Btw, I am aware that I could use a variable in my velocity code to sum the contents of the column, but I prefer to use the simpler, built-in Word formula.)</p>