In a Perl CGI, using Win32::OLE, I need to insert several text lines in a table cell, but one by one.
The different objects I use are :
- $go_document, the Word document
- $lo_table, the Word table
- $li_row and $li_col, the cell coordinates in the table
- @ly_lines, the text array
Here's my code, which works properly:
for (my $ii = 0; $ii <= $#ly_lines; $i++)
{
my $lo_range = $lo_table->Cell($li_row, $li_col)->Range;
$lo_range->InsertAfter($ly_lines[$ii]);
$lo_range = $lo_table->Cell($li_row, $li_col)->Range;
$lo_range->InsertParagraphAfter();
}
My problem happens when I try to apply a style to each one of the text lines.
Since I don't want to set the style of the entire cell but of the line I'm about to insert, I tried this (the styles being stored in the @ly_styles array):
for (my $ii = 0; $ii <= $#ly_lines; $i++)
{
my $lo_range = $lo_table->Cell($li_row, $li_col)->Range;
$lo_range->Collapse(wdCollapseEnd);
$go_document->Styles->Add($ly_styles[$ii]);
$lo_range->{'Style'} = $ly_styles;
$lo_range->InsertAfter($ly_lines[$ii]);
$lo_range = $lo_table->Cell($li_row, $li_col)->Range;
$lo_range->Collapse(wdCollapseEnd);
$lo_range->InsertParagraphAfter();
}
With this loop, the text lines read in a reversed order and in the cell next to the one I want!
It seems that the Collapse(wdCollapseEnd) command doesn't set the range to the end of the aimed cell, but to the beginning of the next one.
Does anyone know how to fix this? Thanks in advance.