none
Copy and Paste word doc with .net C# that contains Footers/Headers RRS feed

Answers

  • Which information in that somewhat long and convoluted discussion did you implement?

    Paragraph Spacing After is not part of Page Layout. Could you please be more precise what it is?

    As mentioned somewhere in that discussion, the default behavior when you copy/paste is that Word will use the style definitions in the target document. There are Header and Footer styles, but I can well imagine these won't be transferred even when selecting to retain the original format. This is because the Header/Footer information is stored in the SECTION BREAK(s) of what's being copied. These are handled differently than text being pasted into the body of the document and it's possible that wdFormatOriginalFormatting (assuming that's what you're using) doesn't work on that information.

    Possibly, your code will need to compare the Header and Footer style definitions of the two documents and, should they differ, create new style definitions in the source document, format the header/footer with those, copy, paste, then undo the changes made in the source document - NOT a trivial undertaking.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 8, 2013 9:13 AM
    Moderator

All replies

  • Which information in that somewhat long and convoluted discussion did you implement?

    Paragraph Spacing After is not part of Page Layout. Could you please be more precise what it is?

    As mentioned somewhere in that discussion, the default behavior when you copy/paste is that Word will use the style definitions in the target document. There are Header and Footer styles, but I can well imagine these won't be transferred even when selecting to retain the original format. This is because the Header/Footer information is stored in the SECTION BREAK(s) of what's being copied. These are handled differently than text being pasted into the body of the document and it's possible that wdFormatOriginalFormatting (assuming that's what you're using) doesn't work on that information.

    Possibly, your code will need to compare the Header and Footer style definitions of the two documents and, should they differ, create new style definitions in the source document, format the header/footer with those, copy, paste, then undo the changes made in the source document - NOT a trivial undertaking.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 8, 2013 9:13 AM
    Moderator
  • Hello,

    Here is my solution to copy document with header/footer to a new document. But it uses a C# Word component instead of Mircosoft.Office.Interop to realize this function easily.

                //Load Document with Header/Footer
                Document document = new Document("HeaderFooter.docx");
                //Create New Document with Contents Cloned by the First Document
                Document doc2 = document.Clone();
                //Add Section in New Document
                Section section = doc2.AddSection();
                //Save New Document
                doc2.SaveToFile("test.docx", FileFormat.Docx);
    Hope Helpful!

    • Edited by Jinasoso Thursday, April 11, 2013 3:33 AM
    Thursday, April 11, 2013 3:32 AM