Organisations usually have tons of Word documents made by using a template. If this template is later changed, you need to update all existing documents somehow. I faced this issue when migrating from SharePoint onprem to SharePoint Online, and the document template was using SharePoint Column data in document headers. All the document headers were broken after migration because none of the existing migration tools actually updates the document content (and template) to mach the new environment. In order to fix this I used ShareGate to export all the documents to disk, then updated the documents to a new template by using PowerShell, and finally used ShareGate to import the documents to SharePoint Online. PowerShell script consists of following steps:
- Open existing document and copy all the content
- Create a new empty document by using the new document template
- Paste all content and save
And here is the script:
$source = "C:\Migration\Source.docx" $destination = "C:\Migration\Destination.docx" $template = "C:\Migration\Template.dotx" [ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type] # Open Word $word = New-Object -comobject word.application $word.visible = $false $word2 = New-Object -ComObject word.application $word2.visible = $false # Create new from template $doc = $word.documents.open($source) $doc2 = $word2.Documents.add($template) # Copy $range = $doc.Range() $copy = $range.Copy() # Paste $range2 = $doc2.Range() $range2.Paste($copy) # Save $doc2.saveas([ref]$destination, [ref]$saveFormat::wdFormatDocument) # Close Word $word.Application.ActiveDocument.Close() $word.Quit() $word2.Application.ActiveDocument.Close() $word2.Quit()
Hopefully this will help you to update Word document templates if needed.