Perl Win32::OLE word merging two doc files

0

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.

perl
asked on Stack Overflow Feb 24, 2015 by Paddy • edited Feb 24, 2015 by Paddy

1 Answer

0

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
answered on Stack Overflow Feb 24, 2015 by ChatterOne

User contributions licensed under CC BY-SA 3.0