- #!/usr/bin/perl
- #use Acme::PerlTidy;
- use Win32::OLE;
- use Win32::OLE::Const 'Microsoft.Word'; # wd constants
- my $word = Win32::OLE->new("Word.Application");
- $word->{Visible} = 1;
- my $document = $word->Documents->Add;
- my @t;
- for ( 0 .. 3 ) {
- my $Range = $document->ActiveWindow->Selection->{Range};
- my $table = make_table( $document, $Range );
- $document->ActiveWindow->Selection->EndKey( { unit => wdStory } );
- enter($document);
- push( @t, $table );
- }
- $t[0]->cell( 1, 1 )->Range->InsertAfter("表格一单元格一");
- $t[1]->cell( 1, 1 )->Range->InsertAfter("表格二单元格一");
- $document->SaveAs('d:/test.doc');
- $document->close;
- $word->quit;
- sub make_table {
- my ( $document, $Range ) = @_;
- my $objTa = $document->Tables->Add( $Range, 3, 2 );
- $objTa->Columns(1)->{Width} = '5';
- $objTa->Borders->{InsideLineStyle} = wdLineStyleSingle;
- $objTa->Borders->{OutsideLineStyle} = wdLineStyleSingle;
- $objTa->Cell( 1, 1 )->Merge( { MergeTo => $objTa->Cell( 3, 1 ) } );
- return $objTa;
- }
- sub enter {
- my $document = shift;
- $document->ActiveWindow->Selection->TypeParagraph;
- }
automation 方法和属性
一旦你创建了一个 automation 对象,你就可以调用对象的方法,如果需要你还可以调整对象的属性。automation 对象方法调用用perl语法来表示:
$obj->some_method( args );
automation 对象方法通常都会需要很多参数。如果你不想要任何一个参数,可以在参数表里用undef代替。
$xl->workbooks(1)->saveas( $f , undef , undef , undef , undef , 1 , undef , undef , 1 );
你也可以用一个包含命名参数赋值的hash 散列的引用来作为参数。上面这个例子也可以这样写:
$xl->workbooks(1)->saveas( $f , {addtomru => 1 , createbackup => 1});
automation 对象的属性可以用hash符号来访问。比如:
$value = $obj->{"property"};
$obj->{"property"} = $value;
阅读(3550) | 评论(0) | 转发(0) |