Perl Win32 :: OLE word合并两个doc文件

时间:2015-02-24 06:54:41

标签: perl

我正在尝试将两个文档合并到新的doc文件中,但我在下面的代码中遇到错误。

    use strict;
    use Win32::OLE;
    use Win32::OLE::Const 'Microsoft Word';

    $meta_file_path = "D:\\copyfrom.doc";
    $main_file_path = "D:\\copyto.doc";

    my $x = Win32::OLE->GetActiveObject('Word.Application') ;
    my $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; } );
    my $doc = $word->Documents->open($meta_file_path) or die $!;

        my $doc = $word->Documents->Open($meta_file_path) or die $!;
$word->ActiveDocument->Content;
$word->ActiveDocument->Select();
$word->Selection->Copy();
$doc->Close;
my $doc2 = $word->Documents->Open($main_file_path) or die $!;       
$word->ActiveDocument->Content->Paste();
$word->ActiveDocument->SaveAs('D:\\outdoc.doc');
$doc2->Close;       
exit;

错误是:

Win32::OLE(0.1709) error 0x80020011: "Does not support a collection"
    in METHOD/PROPERTYGET "" at D:/merge.pl line 12.
Can't call method "Content" on an undefined value at D:/merge.pl line 12.

我从perlmonks得到了一个代码,它给了我欲望输出,但它们是格式中的一个问题,copyfrom.doc有不同对齐的表和文本以及copyto.doc的字体系列。

1 个答案:

答案 0 :(得分:0)

您可能尝试将当前文档用作Documents集合的一部分,而它实际上是Word应用程序对象的一部分。 我没有办法在这里测试代码,但尝试更改

$doc->ActiveDocument

$word->ActiveDocument