in reply to Win32 OLE Word combining docs

MSDN Word Range.Copy gives an example of copying a paragraph, and pasting it in the same document.

ActiveDocument.Paragraphs(1).Range.Copy Set myRange = ActiveDocument.Range _ (Start:=ActiveDocument.Content.End - 1, _ End:=ActiveDocument.Content.End - 1) myRange.Paste

I think the same should work to copy from one document to another in VBA, just change make the copy-range be from $inputdoc and the paste-range be in $outputdoc (and perl-ify the VBA, of course)

Replies are listed 'Best First'.
Re^2: Win32 OLE Word combining docs
by cormanaz (Deacon) on Aug 24, 2016 at 00:00 UTC
    ++Thank you that did it. The working code is below, with a couple of mods to put a page break between files and put the file name in a heading at the start.

    If I may be permitted a small rant: What kind of twisted mind came up with this Office object model? It is the Rube Goldberg Device of object models. Six steps to insert a heading. REALLY? It took me forever to figure out that you make a selection in the document, but then the selection belongs to to system, not that document. Geez.

    #!/usr/bin/perl -w use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $word = Win32::OLE->new('Word.Application') or die "Failure - word. + \n"; $word->{'Visible'} = 1; my $outputdoc = $word->Documents->Add; my @inputdocs = qw( foo.doc bar.doc); foreach my $f (@inputdocs) { my $ip = $outputdoc->Range({Start=>$outputdoc->Content->End -1,End +=>$outputdoc->Content->End -1}); $ip->Select(); my $selection = $word->Selection; $selection->TypeText($f); $selection->{'Style'} = "Heading 2"; $selection->TypeParagraph; my $inputdoc = $word->Documents->Open({FileName => "c:\\temp\\work +\\$f"}); my $inputparagraphs = $inputdoc->Paragraphs; my $nparagraphs = $inputdoc->Paragraphs->Count; for my $i (1..$nparagraphs) { $inputdoc->Paragraphs($i)->Range->Copy; my $outrange = $outputdoc->Range({Start=>$outputdoc->Content-> +End -1,End=>$outputdoc->Content->End -1}); $outrange->Paste; } $inputdoc->close; my $outputend = $outputdoc->Range({Start=>$outputdoc->Content->End + -1,End=>$outputdoc->Content->End -1}); $outputend->InsertBreak(); } $outputdoc->SaveAs({FileName => 'c:\\temp\\work\\combined.doc'});