Merge document

Add-in Express™ Support Service
That's what is more important than anything else

Merge document
Word 2007 merge documents 
Michiel van Ingen


Guest


Hello All,

Word 2007 has a nice feature to combine two documents, does anyone know how to do this trough code, in VBA it's:

Application.MergeDocuments OriginalDocument:=Documents("Versie 1.docx"), _
RevisedDocument:=Documents("Versie 2.docx"), Destination:= _
wdCompareDestinationNew, Granularity:=wdGranularityWordLevel, _
CompareFormatting:=True, CompareCaseChanges:=True, CompareWhitespace:= _
True, CompareTables:=True, CompareHeaders:=True, CompareFootnotes:=True, _
CompareTextboxes:=True, CompareFields:=True, CompareComments:=True, _
CompareMoves:=True, OriginalAuthor:="Windows-gebruiker", RevisedAuthor:= _
"Windows-gebruiker", FormatFrom:=wdMergeFormatFromPrompt

I'm building my addin in Delphi 2007, but I can't seem to find out how to call the mergeDocuments function, any help would be greatly appreciated!

Best regards,
Michiel van Ingen
Posted 28 Mar, 2011 09:09:06 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Michiel,

You can get access to any methods introduced in the newest Word versions by using late binding. Please see the sample code below:


var
  Doc1, Doc2: WordDocument;
  Index: OleVariant;
begin
  // Word 2007
  if Self.HostMajorVersion >= 12 then begin
    Index := 1;
    Doc1 := WordApp.Documents.Item(Index);
    Index := 2;
    Doc2 := WordApp.Documents.Item(Index);

    OleVariant(WordApp.DefaultInterface).MergeDocuments(
      Doc1,       // OriginalDocument
      Doc2,       // RevisedDocument
      2,          // Destination (wdCompareDestinationNew = 2)
      1,          // Granularity (wdGranularityWordLevel = 1)
      True,       // CompareFormatting
      True,       // CompareCaseChanges
      True,       // CompareWhitespace
      True,       // CompareTables
      True,       // CompareHeaders
      True,       // CompareFootnotes
      True,       // CompareTextboxes
      True,       // CompareFields
      True,       // CompareComments
      True,       // CompareMoves
      'Author 1', // OriginalAuthor
      'Author 2', // RevisedAuthor
      2           // FormatFrom (wdMergeFormatFromPrompt = 2)
    );

    Doc1 := nil;
    Doc2 := nil;
  end;
end;
Posted 28 Mar, 2011 11:49:49 Top
Michiel van Ingen


Guest


Hi Dmitry,

That works like a charm!!, thanks you very much for your help! :D
Posted 28 Mar, 2011 13:04:01 Top