title: Table in Word looks different if halted on breakpoint
body: <p>I have stumbled on a very annoying problem when setting column widths on a table in Word (using Microsoft Office 15.0 Object Library, VS 2013). If I run the code below without having any breakpoints, the result becomes incorrect (first column width should be 30%), but if I put a breakpoint (e.g.) on line 47, the generated Word file becomes as I want it to be.</p>
  
  <p>The conclusion I make is that when the debugger stops execution, the given column size values are flushed into the data model and the output will be as I want it to be. Without the breakpoint, the merging of cells changes the widths (e.g. the first column becomes 12,5%).</p>
  
  <p>I have searched for some sort of method/function to make the data model adjust to my programmatically given column sizes before the cell merging, with no luck. Anyone out there that can explain why halting on the breakpoint will change the output?</p>
  
  <p>Regards,
  Ola</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Runtime.InteropServices;
  using Microsoft.Office.Interop.Word;
  
  namespace ShowWordProblem
  {
      class Program
      {
          private const string WordFileName = @"C:\temp\test.doc";
          static void Main(string[] args)
          {
              var wordApplication = new Application();
              wordApplication.Visible = false;
              var wordDocument = wordApplication.Documents.Add();
  
              FillDocument(wordDocument);
  
              wordDocument.SaveAs2(WordFileName);
              wordDocument.Close();
  
              wordApplication.Quit(Type.Missing, Type.Missing);
              Marshal.FinalReleaseComObject(wordApplication);
  
              wordApplication = null;
              wordApplication = new Microsoft.Office.Interop.Word.Application();
              wordApplication.Visible = true;
              wordApplication.Documents.Open(WordFileName);
          }
  
          private static void FillDocument(Document wordDocument)
          {
              Range range = wordDocument.Content.Paragraphs.Add().Range;
  
              var table = range.Tables.Add(range, 5, 8);
              table.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthPercent;
              table.PreferredWidth = (float)100.0;
              table.Columns.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthPercent;
  
              table.Columns.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthPercent;
              table.Columns[1].PreferredWidth = 30;
              for (int i = 2; i &lt;= 8; i++) table.Columns[i].PreferredWidth = 10;
  
              var widths = table.Columns.OfType&lt;Column&gt;().Select(c =&gt; c.Width).ToArray();
  
              MergeGroupHeaderCells(table.Rows[1], 5, 9);
              MergeGroupHeaderCells(table.Rows[1], 2, 5);
          }
  
          private static void MergeGroupHeaderCells(Row row, int startIndex, int lastIndex)
          {
              var r = row.Cells[startIndex].Range;
              Object cell = WdUnits.wdCell;
              Object count = lastIndex - startIndex - 1;
              r.MoveEnd(ref cell, ref count);
              r.Cells.Merge();
          }
      }
  }
  </code></pre>