3
votes
#!C:\Perl\bin\perl.exe
use warnings;
use 5.012;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 3;

my $word;
eval{ $word = Win32::OLE->GetActiveObject( 'Word.Application' ); };
die 'Word not Installed' if $@;
if ( not defined $word ) {
    $word = Win32::OLE->new( 'Word.Application', 'Quit' ) or die 'Cannot start Word: ', Win32::OLE->LastError(); 
}
die 'Word -> Documents is unavailable' if not $word -> Documents;

my $file = 'testword';
my $doc = $word -> Documents -> Add() or die Win32::OLE->LastError();;
my $select = $word -> Selection;

my $table = $doc->Tables->Add( { Range => $select->{Range}, NumRows => 10, NumColumns => 2 } );

$select->MoveRight( { Unit => wdCell, Count => 1 } ); # $table->Cell( { Row => 0, Column => 1 } )
$select->fields->Add( { Range => $select->Range, Type => wdFieldMacroButton, Text => 'NoName [Vorname]' } );
$select->MoveDown( { Unit => wdLine, Count => 1 } ); # $table->Cell( { Row => 1, Column => 1 } )
$select->fields->Add( { Range => $select->Range, Type => wdFieldMacroButton, Text => 'NoName [Nachname]' } );
$select->MoveDown( { Unit => wdLine, Count => 1 } ); # $table->Cell( { Row => 2, Column => 1 } )
$select->fields->Add( { Range => $select->Range, Type => wdFieldMacroButton, Text => 'NoName [Strasse Nr]' } );

$doc -> SaveAs( { FileName => $file, FileFormat => wdFormatTemplate } ); 
$doc -> Close( );

How could I here move around in the table with indicating the cell-row and the cell-column instead of using $select->MoveRight, $select->MoveDown, ... ?

1

1 Answers

-1
votes

First, get a cell object (for example: $cell = $table->Cell( { Row => 0, Column => 1 } )), then use $cell->Select( ) to select it.

Reference: http://msdn.microsoft.com/en-us/library/bb241174%28v=office.12%29.aspx