Programatically insert a Word document into an existing document (Word 2007)

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.

I'm doing this using the Word API in C#.

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.

OK then, I'll use copy and paste instead, like so...

_Document sourceDocument = wordApplication.Documents.Open(insert the 8 million by ref parameters Word requries)
sourceDocument.Activate(); // This is the document I am copying from 
targetDocument.Activate(); // This is the document I am pasting into

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.

However - now this is where I have the problem. The paste only includes the borders, header etc if I paste at the end 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.


Would the bookmark functionality work. The InsertFile contains parameters to take from this which may get around the problem. You may have considered this already though

I'm actually working on something similar at the moment strangely enough and found a powershell cmdlet library written in C# that you might find useful:

Powertools for Open XML

It is still a little buggy with inherited headers and footers as well as with image references not being copied correctly if the same image is in multiple parts of the document, but a lot of the structure is in place.

This sort of worked for me. Still looking for a better solution. Be sure to add your reference to Interop.Word. I know this is an old thread and I am using Word 2016 but I searched for a long time and had to piece the solution together.

using Word = Microsoft.Office.Interop.Word;
var wordApp = new Word.Application();
wordApp.Visible = true;
//Open is for an existing document. 
//Add is to use a template.
//Get the range to be able to then collapse and have the correct insertion point
var rng = wordApp.ActiveDocument.Range();

TO INSERT A WORD DOCUMENT INTO ANOTHER WORD 2007 DOCUMENT The only way I found successful for WORD 2007 is... open the document you want to add the pages to, then goto "Insert" tab on the ribbon, look at the the "text" section (same place as text box, word art, etc) and select "object", a drop down menu appears, then select "Text from File". From here you simply select the document you want inserted and its done, you may need to make some slight adjustment, but it all there formatted correctly, be sure to place your cursor at the point you want the new stuff inserted. Hope this helps

Need Your Help

What to do when your ant build process craps on your Version Control

svn ant jarbundler

So I've got a nice Java project, build with Ant to a /dist folder.