Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramatically insert a Word document into an existing document (Word 2007)
    text
    copied!<p>I have a Word 2007 document that I want to insert an exsiting Word document into - while preserving the header/footer, graphics, borders etc of both documents.</p> <p>I'm doing this using the Word API in C#.</p> <p>It sounds pretty simple, I mean surely you just use the "InsertFile" method... except that in Word 2007 the "insert file" functionality now is actually "insert text from file" and it does just that - leaving out the page border, graphics and footer etc.</p> <p>OK then, I'll use copy and paste instead, like so...</p> <pre><code>_Document sourceDocument = wordApplication.Documents.Open(insert the 8 million by ref parameters Word requries) sourceDocument.Activate(); // This is the document I am copying from wordApplication.Selection.WholeStory(); wordApplication.Selection.Copy(); targetDocument.Activate(); // This is the document I am pasting into wordApplication.Selection.InsertBreak(wdSectionBreakNextPage); Selection.PasteAndFormat(wdFormatOriginalFormatting); wordApplication.Selection.InsertBreak(wdSectionBreakNextPage); </code></pre> <p>which does what you would expect, takes the source document, selects everything, copies it then pastes it into the target document. Because I've added a section break before doing the paste it also preserves the borders, header/footer of both documents.</p> <p>However - <strong>now this is where I have the problem.</strong> The paste <em>only</em> includes the borders, header etc if I paste at the <em>end</em> of the target document. If I paste it in the middle - despite there being a preceding section break, then only the text gets pasted and the header and borders etc are lost.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload