Chinaunix首页 | 论坛 | 博客
  • 博客访问: 221838
  • 博文数量: 36
  • 博客积分: 1188
  • 博客等级: 军士长
  • 技术积分: 802
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 21:45
文章分类

全部博文(36)

文章存档

2020年(1)

2017年(2)

2015年(1)

2014年(1)

2013年(1)

2012年(3)

2011年(27)

分类: WINDOWS

2011-08-31 13:15:35

  1. #!/usr/bin/perl
  2. #use Acme::PerlTidy;
  3. use Win32::OLE;
  4. use Win32::OLE::Const 'Microsoft.Word'; # wd constants
  5. my $word = Win32::OLE->new("Word.Application");
  6. $word->{Visible} = 1;
  7. my $document = $word->Documents->Add;

  8. my @t;
  9. for ( 0 .. 3 ) {
  10.     my $Range = $document->ActiveWindow->Selection->{Range};
  11.     my $table = make_table( $document, $Range );
  12.     $document->ActiveWindow->Selection->EndKey( { unit => wdStory } );
  13.     enter($document);
  14.     push( @t, $table );
  15. }
  16. $t[0]->cell( 1, 1 )->Range->InsertAfter("表格一单元格一");
  17. $t[1]->cell( 1, 1 )->Range->InsertAfter("表格二单元格一");

  18. $document->SaveAs('d:/test.doc');
  19. $document->close;
  20. $word->quit;

  21. sub make_table {
  22.     my ( $document, $Range ) = @_;
  23.     my $objTa = $document->Tables->Add( $Range, 3, 2 );
  24.     $objTa->Columns(1)->{Width} = '5';
  25.     $objTa->Borders->{InsideLineStyle} = wdLineStyleSingle;
  26.     $objTa->Borders->{OutsideLineStyle} = wdLineStyleSingle;
  27.     $objTa->Cell( 1, 1 )->Merge( { MergeTo => $objTa->Cell( 3, 1 ) } );

  28.     return $objTa;
  29. }

  30. sub enter {
  31.     my $document = shift;
  32.     $document->ActiveWindow->Selection->TypeParagraph;
  33. }
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;
阅读(3491) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~