简体   繁体   中英

Perl Win32::OLE word merging two doc files

I'm trying to merge two document into new doc file but I'm getting error in following code.

    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;

Error is :

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.

I got a code from perlmonks which give me desire output but their is one problem in format, copyfrom.doc has tables and text with different alignment and font family from copyto.doc.

You're probably trying to use the current document as part of the Documents collection, while it's actually part of the Word application object. I don't have the means to test the code here, but try changing

$doc->ActiveDocument

to

$word->ActiveDocument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM