LameNerd has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to write a perl script that will concatentate N number of MS Word documents into one big-ole word doc?
Thanks
  • Comment on cat worddoc1.doc worddoc2.doc worddoc3.doc > bigOlWordDoc.doc

Replies are listed 'Best First'.
Re: cat worddoc1.doc worddoc2.doc worddoc3.doc > bigOlWordDoc.doc
by meetraz (Hermit) on Jan 24, 2003 at 19:54 UTC
    I'm not sure on the exact syntax, but it would work something like this:
    ### UNTESTED use strict; use Win32::OLE; my $MSWord = Win32::OLE->new('Word.Application'); my $WordDoc1 = $MSWord->{Documents}->Open("doc1.doc",0,1); my $WordDoc2 = $MSWord->{Documents}->Open("doc2.doc",0,1); my $WordDoc3 = $MSWord->{Documents}->Open("doc3.doc",0,1); my $WordDoc4 = $MSWord->{Documents}->Open("bigdoc.doc",0,1); $WordDoc1->SelectAll()->Copy(); $WordDoc4->Paste(); $WordDoc2->SelectAll()->Copy(); $WordDoc4->Paste(); $WordDoc3->SelectAll()->Copy(); $WordDoc4->Paste(); $WordDoc4->Save(); $WordDoc1->Close(): $WordDoc2->Close(): $WordDoc3->Close(): $MSWord->Quit();

    more info here

      I can't seem to get this line to work.
      my $WordDoc1 = $MSWord->{Documents}->Open($doc1,0,1);
      $WordDoc1 is undefined after that statement so
      $WordDoc1->SelectAll()->Copy();
      fails with
      Can't call method "SelectAll" on an undefined value at C:\catword.pl line
      Thanks for that link but I do not know how to translate VB to Perl.
      Can you give me a little more help?
        Do you have MSWord installed? Is $MSWord defined before you try the open() ?
Re: cat worddoc1.doc worddoc2.doc worddoc3.doc > bigOlWordDoc.doc
by poj (Abbot) on Jan 25, 2003 at 11:47 UTC
    Try this (tested using Word97 with a document having 10 text lines and a graphic)
    use strict; use Win32::OLE; my $MSWord = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); my $bigDoc = $MSWord->Documents->Open("big.doc") or die "Could not open big.doc\n"; # $MSWord->{'Visible'} = 1; # uncomment this to see it work # list of documents to include my @documents = qw(test1.doc test1.doc test1.doc test1.doc); # copy each document into the big one for (@documents){ my $sourceDoc = $MSWord->Documents->Open("$_") or die "Could not open $_\n"; $sourceDoc->Select; $MSWord->Selection->Copy; $sourceDoc->Close(); $MSWord->Selection->Paste; print "$_ copied\n" } $bigDoc->Close(); $MSWord->Quit(); print "Program ended\n";
    poj