1
votes

I am trying to copy a word document to another without closing the target document. So, I do it like that:

temp_doc_.Content.Copy();
target_doc_.Content.Paste();
temp_doc_.Close(SaveChanges: false);

However, this doesn't copy some features like margins, header height, ... etc. It only copies section paragraphs. Does anyone know how to do a full copy so that they are really identical in everything ?

3
To see what's required for that, visit: msofficeforums.com/word-vba/…. The code there is VBA, but that will give you and idea of what's involved.macropod

3 Answers

0
votes

I'm not 100% sure this will do the margins, but it WILL do the headers and footers.

Word.Document tempDoc = wordApp.Documents.Open('tempdoc.docx');
Word.Document targetDoc = wordApp.Documents.Open('targetdoc.docx');

tempDoc.Activate();
tempDoc.Application.Selection.WholeStory();
tempDoc.Application.Selection.Copy();

targetDoc.Activate();
targetDoc.Application.Selection.WholeStory();
targetDoc.Application.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting);
0
votes

You can copy the open document without closing it, and without affecting it's "saved"-state.

filePath could be in the temp-folder or similar.

sourceDocument must be a Microsoft.Word.Document instance.

VB

Dim persistFile = CType(sourceDocument, Runtime.InteropServices.ComTypes.IPersistFile)
persistFile.Save(filePath, False)

C#

var persistFile = (Runtime.InteropServices.ComTypes.IPersistFile)sourceDocument;
persistFile.Save(filePath, False);

Then just open the copied file and you have a complete clone of the entire document.