Win32 :: OLE - Word:如何在表中移动?

时间:2011-08-18 18:35:45

标签: perl ms-word win32ole

#!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( );

我如何在表格中移动并指出cell-rowcell-column,而不是使用$select->MoveRight$select->MoveDown,...?

1 个答案:

答案 0 :(得分:-1)

首先,获取一个单元格对象(例如:$cell = $table->Cell( { Row => 0, Column => 1 } )),然后使用$cell->Select( )选择它。

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