set column width in word automation throws an exception

Maha Khairy picture Maha Khairy · Apr 11, 2012 · Viewed 8.3k times · Source

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);

Answer

Kiru picture Kiru · Apr 24, 2012

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;