none
Pasting Header and/or Footers in MS Word Links to Previous RRS feed

  • Question

  • I am using VBA to copy two sections from one Word document and paste into a new Word document.  All the text from the Main body of the document pastes successfully.  However, since there are two sections there are two headers and footers.  In the first section the footer is blank but in the second section there is text in the footer.  When I paste the sections into the new document it automatically links the footer in the second section to the footer in the first section.  Since there is no footer in the first section, the footer in the section is erased.  Is there any way to switch off the Link to Previous option in the Header/Footer before or during the paste operation? Or will I have to paste the Main body and header/footer separately?
    • Edited by kaykayme Friday, July 26, 2013 12:20 PM add a word
    Friday, July 26, 2013 12:19 PM

Answers

  • I created a work around for this problem.  I added a content control with the Footer text to the document and change the font color to white.  The text in the footer could change so I went with this option.  When I have copied and pasted the section I set the LinkToPrevious to False then paste the text of the content control into the footer of the second section.  I also had a problem with the last section of the document.  Even when I programmatically selected the section with the section break it did not copy the header and footer.  So I just created the template that I paste into with the header and footer since the text in this is static and just pasted the Main text in the new document. Just wish there were an easier way.
    Friday, July 26, 2013 6:43 PM
  • While still working with this issue I found a solution to the problem of copying sections including the headers and footers and pasting into a new document.  Since I am running the code from Access to create the Word documents I had to make the variable for the Word object and Word document Static instead of creating a new Word object and document when calling the function to separate and save the sections into a new Word document.  Prior to this I was running the code to separate and save the sections in the Word document itself and not in Access.

    • Marked as answer by kaykayme Tuesday, August 6, 2013 12:26 PM
    Tuesday, August 6, 2013 12:26 PM

All replies

  • I think you will always have a potential problem with this unless you copy by section and eliminate the last paragraph mark from each section you are copying. This is because the headers and footers of a document are attached to the last paragraph mark in each section.

    So for your example, copying everything from the source document except the last paragraph mark would accomplish what you are trying to do. Unfortunately, that will only work for two section documents where the first section footer is blank. If you have a two section document that you want to copy and its first section footer is not blank... you have the same problem again. Thus you must copy by section and eliminate the last paragraph mark.

    An alternative is to clear the headers and footers in all sections of the source document before you copy and then close the source file without saving it.


    Kind Regards, Rich ... http://greatcirclelearning.com


    Friday, July 26, 2013 1:55 PM
  • Hi Kay

    If you copy the section breaks with the text (and you must be if the header/footer information is coming across), then I would expect the "Link to Previous" to come in with the section breaks...

    Which version of Word is this?

    What's the section situation in the target document?

    Can you explain why you'd be copying and pasting, rather than using Building Blocks/AutoText or possibly InsertFile?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 26, 2013 1:56 PM
    Moderator
  • Thank you Cindy for your response.  I would agree with you as far as copying the section breaks but this is not what is taking place.  When I paste the sections with the Section breaks it automatically reverts to Links to Previous even though the source document does not have this option set to true.

    I am using Word 2010.

    The reason I am copying the sections and pasting in a new document is because I have several related files in one document.  I am using a Userform to gather information.  The userform has a button that runs code to insert text into Content Controls.  Some of the exact same Content Controls are on each of the documents so by using SelectContentControlbyTitle and looping through each Content Control with the same title I can populate each document separated by section breaks with the same information.  This eliminates the need for the user to open every document to populate the data, enter once - populate all.  Once the data has been populated the code copies the section(s) into a new document and saves separately.  This has worked fine for some time with two documents but now I am adding 2 more documents to the original template.  All the other documents separate fine but this will not because of the footer issue.

    I am not sure that using Building Blocks/AutoText or InsertFile will accomplish this.

    Friday, July 26, 2013 2:12 PM
  • You may also want to consider the article at http://word.mvps.org/faqs/formatting/workwithsections.htm.


    Stefan Blom, Microsoft Word MVP

    Friday, July 26, 2013 6:27 PM
  • Thank you Stefan.  My company blocks access to this site.

    Friday, July 26, 2013 6:29 PM
  • I created a work around for this problem.  I added a content control with the Footer text to the document and change the font color to white.  The text in the footer could change so I went with this option.  When I have copied and pasted the section I set the LinkToPrevious to False then paste the text of the content control into the footer of the second section.  I also had a problem with the last section of the document.  Even when I programmatically selected the section with the section break it did not copy the header and footer.  So I just created the template that I paste into with the header and footer since the text in this is static and just pasted the Main text in the new document. Just wish there were an easier way.
    Friday, July 26, 2013 6:43 PM
  • The link that Stefan shared has some excellent ideas about using temporary section breaks in the documents that your are copying from or pasting into. You might try the following for your situation:

    At the end of the destination document add a temporary section break. Place the insertion point at the End of Document. Now instead of using Copy/Paste use the Selection.InsertFile("C:\\test.docx"). This brings in the entire file and you don't have to open the source file.

    Now in the headers and footers from the last two sections that you just added, Link to Previous = True, which wipes out the H/F text from inserted file. And the final thing to do is delete the temporary section break and the section break brought over from the insert operation.

    Try it out manually first and I think you'll see the sequence of things you need to do programmatically. And I believe it will be cleaner and easier than the content control workaround.

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, July 26, 2013 7:28 PM
  • Thank you Rich.  This seems like a little more work because the source file contains 4 documents separated by section breaks.  Each of the documents (sections) have their own headers and footers.  Some of the sections have different first page. It is necessary to have the source file open because it is a new document from the template with data being inserted from a Userform and has not been saved at this point.  Before saving the source file, I am copying three of the sections and saving the sections as new documents.  The sections are then deleted from the source file and then the source file is saved.  I don't think the solution of InsertFile will work.
    Friday, July 26, 2013 7:39 PM
  • While still working with this issue I found a solution to the problem of copying sections including the headers and footers and pasting into a new document.  Since I am running the code from Access to create the Word documents I had to make the variable for the Word object and Word document Static instead of creating a new Word object and document when calling the function to separate and save the sections into a new Word document.  Prior to this I was running the code to separate and save the sections in the Word document itself and not in Access.

    • Marked as answer by kaykayme Tuesday, August 6, 2013 12:26 PM
    Tuesday, August 6, 2013 12:26 PM