none
Copying word document header and footer from one document to another with PowerShell RRS feed

  • Question

  • Hi,

    we have a case where we would need to update thousands of documents and we would need to change the header and footer. We have new template document which I would like to use to copy the new header and footer from.

    I tried to do that via PowerShell, but could not find a way to copy (header/footer) from template to existing document. The case may be that the old document may have header and footer or they might be empty.

    I would love to get some help on this :)

    Wednesday, November 4, 2015 8:14 AM

Answers

  • Hi,

    I found now way to do that with using the Open-XML-PowerTools from

    https://github.com/OfficeDev/Open-Xml-PowerTools. With you have possibilty to utilize PowerShell to loop through the documents and then use OXPT commandlets

    Below is example for single document. TemplateDoc.docx is the one where I have my header and footer and OriginalDoc.docx is the one where I want to update the header/footer. By using the Merge-docx I create new document where I have header from template and contents from original document.

    $templatedocpath = Get-Item "TemplateDoc.docx"
    $templateDoc = New-Object OpenXmlPowerTools.Source($templatedocpath.fullname,$false)
    
    $orignaldocpath = Get-Item "OriginalDoc.docx"
    $orignaldoc = New-Object OpenXmlPowerTools.Source($orignaldocpath.FullName,$false)
    
    $newdoc = Merge-Docx -Sources ($templateDoc,$orignaldoc)
    $newdocpath = "NewDoc.docx"   
    $newdoc.SaveAs($newdocpath)


    Thank you for all who pointed me to right direction.

    -Kalle


    • Marked as answer by Kalle Järvi Thursday, November 12, 2015 4:49 PM
    • Edited by Kalle Järvi Thursday, November 12, 2015 4:50 PM text correction
    Thursday, November 12, 2015 4:49 PM

All replies

  • Hi,

    This is the forum to discuss questions and feedback for Microsoft Word, the issue is more related to Word coding/programing, you'd better post your question to the MSDN forum for Word

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=worddev&filter=alltypes&sort=lastpostdesc

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    George Zhao
    TechNet Community Support


    Please mark the reply as an answer if you find it is helpful.

    If you have feedback for TechNet Support, contact tnmff@microsoft.com.
    Thursday, November 5, 2015 7:35 AM
  • Hi Kalle. Your best bet is probably to leverage the Word Open XML file format, possibly using the Open XML SDK. This approach works on the closed files and will be much faster than opening the documents in the Word application and manipulating them.

    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Thursday, November 5, 2015 10:54 PM
    Moderator
  • Hi Cindy,

    thanks for your reply. Would you have some examples on how to get started. That would help a lot then and then I can see if I work out from that. :)

    Saturday, November 7, 2015 10:10 AM
  • Hi Kalle,

    If you install the Word application on the computer, we also can use Office automation to achieve the goal. Here is an example that copy the page header in the first section to another documents.

    Sub copyHeader(fromDoc As Document, toDoc As Document)
    With fromDoc.Sections(1).Headers(wdHeaderFooterPrimary)
     If .Range.Text <> vbCr Then
    
     .Range.Copy
     toDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paste
     
     Else
        MsgBox "Header is empty"
     End If
    End With
    
    End Sub
    
    Sub testCopyHeader()
    Set doc1 = Application.Documents.Open("C:\doc1.docx")
    Set doc2 = Application.Documents.Open("C:\doc2.docx")
    
    copyHeader doc2, doc1
    End Sub
    

    To test the code sample above, you can create a blank document and copy the code into the ThisDocument module of VBE.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 9, 2015 3:38 AM
    Moderator
  • Hi FEi

    You really would not want to use this approach for "thousands of documents"...


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Monday, November 9, 2015 8:42 PM
    Moderator
  • Hi Kalle

    Here's the link to the documentation on MSDN. You'll find quite a few code samples in the "How do I..." section: https://msdn.microsoft.com/en-us/library/office/bb448854.aspx. There's also useful information at openxmldeveloper.org

    Once you've had a chance to get a feel for the topic, you'll find useful Q&A in the Open XML SDK developer forum: https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=oxmlsdk. This is where to ask follow-up questions when you hit a snag.

    As I recall there are questions there about copying headers/footers.

    Note that you need to distinguish copying a header/footer with just text from copying something containing additional content, such as graphics (logos).


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>


    Monday, November 9, 2015 8:47 PM
    Moderator
  • Hi,

    I found now way to do that with using the Open-XML-PowerTools from

    https://github.com/OfficeDev/Open-Xml-PowerTools. With you have possibilty to utilize PowerShell to loop through the documents and then use OXPT commandlets

    Below is example for single document. TemplateDoc.docx is the one where I have my header and footer and OriginalDoc.docx is the one where I want to update the header/footer. By using the Merge-docx I create new document where I have header from template and contents from original document.

    $templatedocpath = Get-Item "TemplateDoc.docx"
    $templateDoc = New-Object OpenXmlPowerTools.Source($templatedocpath.fullname,$false)
    
    $orignaldocpath = Get-Item "OriginalDoc.docx"
    $orignaldoc = New-Object OpenXmlPowerTools.Source($orignaldocpath.FullName,$false)
    
    $newdoc = Merge-Docx -Sources ($templateDoc,$orignaldoc)
    $newdocpath = "NewDoc.docx"   
    $newdoc.SaveAs($newdocpath)


    Thank you for all who pointed me to right direction.

    -Kalle


    • Marked as answer by Kalle Järvi Thursday, November 12, 2015 4:49 PM
    • Edited by Kalle Järvi Thursday, November 12, 2015 4:50 PM text correction
    Thursday, November 12, 2015 4:49 PM