Search code examples
c#c#-4.0ms-wordvstoword-automation

set column width in word automation throws an exception


I'm using Word automation to construct a table, I need one column to be very narrow but every time I use Microsoft.Office.Interop.Word.Column.SetWidth method to set the width, it throws "value out of range Exception" it works if I make it a larger value but it is critical to be as small as it could get

someone suggested that the spacing and padding be set to zero, which I did, but it still throws the exception

      table.LeftPadding = 0;
      table.RightPadding = 0;
      table.Spacing = 0;
      table.Columns[4].SetWidth(9f, WdRulerStyle.wdAdjustNone);

Solution

  • SetWidth take parameter in points. Try this

    table.Columns[4].SetWidth(
        Globals.ThisAddIn.Application.MillimetersToPoints(9f), 
        WdRulerStyle.wdAdjustNone);
    

    or use this

    table.Columns[4].PreferredWidth = 9f;