WJJK has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks I want to generate Word documents in Perl that include tables. This is not a problem, but I get stuck when I want to merge 2 or more consecutive cells into a single cell. I can select a single cell using "$Table1->Cell(2, 1)->Range->Select;". Then I want to select the cell below as well and merge these 2 into a single one, but how? Regards, WJJK

Replies are listed 'Best First'.
Re: Word Tables
by wfsp (Abbot) on Sep 23, 2007 at 10:32 UTC
    Hi WJJK

    One way to get an idea of what you need is to record a Word macro that merges two cells and have a look at the code it produces.

    You should be able to translate that back into Perl.

      I did and got the VBA lines Selection.MoveDown Unit = wdLine, Count = 1, Extend = wdExtend Selection.Cells.Merge Unfortunately, I have not been able to translate this into the correct Perl code.
        After some rooting around in the Word VBA docs and studying Using Win32::OLE and Excel - Tips and Tricks I came up with this.

        (needs Word to be open with a document open containing a table)

        #!/usr/bin/perl use strict; use warnings; use Win32::OLE; my $w = Win32::OLE->GetActiveObject('Word.Application'); my $d = $w->ActiveDocument; my $table = $d->Tables(1); my $first_cell = $table->Cell({Row => 1, Column => 1}); my $second_cell = $table->Cell({Row => 1, Column => 2}); $first_cell->merge({MergeTo => $second_cell}); $d->save(1);