Adithyakiran.k has asked for the wisdom of the Perl Monks concerning the following question:

I am creating a word doc which has both text and tables using win32::ole. I am creating the table using the below sample code. Problem is writing the text after the table. I am not able to move the selection to the end of the table. The text I want to write after the table, appears within the table based on the wrapping of the text in the table cell. Please suggest.

Below code works fine

use Win32::OLE; my ($t_row, $t_col) = (3,3); my $word = Win32::OLE->CreateObject("Word.Application"); $word->{Visible} = 1; my $doc = $word->Documents->Add('D:\new.dotx',0,0,-1); my $table = $doc->Tables->Add($word->Selection->Range,$t_row, $t_col); $table->Select(); $table->Cell(1,1)->Range->{Text} = "heading1"; $table->Cell(1,2)->Range->{Text} = "heading2"; $table->Cell(1,3)->Range->{Text} = "heading3"; $table->Cell(2,1)->Range->{Text} = "Data21"; $table->Cell(2,2)->Range->{Text} = "Data22"; $table->Cell(2,3)->Range->{Text} = "Data23"; $table->Cell(3,1)->Range->{Text} = "Data31"; $table->Cell(3,2)->Range->{Text} = "Data32"; $table->Cell(3,3)->Range->{Text} = "Data33"; $word->Selection->MoveDown(5,4); $doc->ActiveWindow->Selection -> TypeText("This line should be after t +he table");

Problem with the Below Code is that it prints the line within the table

use Win32::OLE; my ($t_row, $t_col) = (3,3); my $word = Win32::OLE->CreateObject("Word.Application"); $word->{Visible} = 1; my $doc = $word->Documents->Add('D:\new.dotx',0,0,-1); my $table = $doc->Tables->Add($word->Selection->Range,$t_row, $t_col); $table->Select(); $table->Cell(1,1)->Range->{Text} = "heading1"; $table->Cell(1,2)->Range->{Text} = "heading2"; $table->Cell(1,3)->Range->{Text} = "heading3"; $table->Cell(2,1)->Range->{Text} = "Data21"; $table->Cell(2,2)->Range->{Text} = "Data22"; $table->Cell(2,3)->Range->{Text} = "Data23"; $table->Cell(3,1)->Range->{Text} = "Data31aaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaa"; $table->Cell(3,2)->Range->{Text} = "Data32"; $table->Cell(3,3)->Range->{Text} = "Data33"; $word->Selection->MoveDown(5,4); $doc->ActiveWindow->Selection -> TypeText("This line should be after t +he table");

Replies are listed 'Best First'.
Re: Writing text after creating a table in MS Word using win32::ole
by hdb (Monsignor) on Sep 12, 2013 at 12:41 UTC

    In order to go to the end of the document, you can do the following:

    use Win32::OLE::Const 'Microsoft.Word'; # wd constants $word->Selection->EndKey(wdStory);

    UPDATE: This also works:

    $table->Select(); $doc->ActiveWindow->Selection->MoveDown(wdLine,1); $doc->ActiveWindow->Selection->TypeText("This line should be after the + table");

    Department of Experimental Computer Science

Re: Writing text after creating a table in MS Word using win32::ole
by Anonymous Monk on Sep 12, 2013 at 12:31 UTC

      I am keeping the count of number of rows that I have to create. Here,

      5 is constant value for wdLine

      4 is the number of rows + 1

        This does not work if a cell has multiple rows that also count. If you set the number of rows to move down to a HUGE number, then it works. Quite inelegantly though...