views:

3601

answers:

3

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 
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);

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.

Can anyone help before I buy a grenade and a one way ticket to Redmond...

+1  A: 

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

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.bookmark.insertfile.aspx

Matrim
Thanks for the suggestion - I had not tried InsertFile. I'm giving it a go now but am struggling to get it working...
I don't think it's possible, because I'm not doing a document-level VSTO application, I don't have access to a controlcollection for example that the bookmark object seems to require.
Pity, probably wont help but a working example of the above can be found herehttp://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx
Matrim
That looks really interesting thanks.
looking at that link more closely- it's not using the bookmark method - it's using the standard 'insertfile' method which doesn't work in 2007 (or - I think 2003)
A: 

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.

Chris J
Thanks for letting me know about this, it gives me another avenue to solve these Office issues.
A: 

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

Universal Saint
we are talking about doing it programatically....