yossiz74 yossiz74 - 2 months ago 25
Perl Question

Win32 OLE (perl) - resize table column width to text

I have a Perl script which adds rows with some text to an existing table in a Word document using Win32::OLE.

Now, I would like to have it also resize the table's columns to match the text size (just like double-clicking on the vertical line between two columns)
I know how to set a column to a specific width, but I can't seem to find an API for resizing a column to match the text size.

Can anyone tell me what to look for?

For reference, here is a code snippet from my script:

my $tbl = $document->Tables(1);
my $row = $tbl->Rows->Add();
$row->Cells(1)->Range()->InsertAfter($timestamp);
$row->Cells(2)->Range()->InsertAfter($comment);
$row->Cells(3)->Range()->InsertAfter($username);
# TODO: resize columns to match the content

Answer

Have you tried the Cells.AutoFit() method? e.g. Columns.AutoFit

It seems only supported for Excel 2010 and newer, though.

https://msdn.microsoft.com/en-us/library/office/ff837476.aspx