Solution - Quick Parts error after migration from SharePoint on-premise to O365 SPO

Introduction

In this blog, I will explain how I fixed the issue regarding Quick Parts. In this issue, the link broke between the document in the SharePoint library and the columns, which should appear as document property. This occurred while migrating content from on-premise SharePoint to SPO.
 
What is a Quick Part?
 
Quick Part is a feature in Microsoft Word which, among other things, allows you to insert a SharePoint Library Column as a field in the Document.
For example, the Created By and Modified By columns from the SharePoint library could be shown as data inside the document which could be handy for some use cases.
 
 
Problem Statement  
 
While this function has existed since Office 2010, I have never come across it being used until now.
 
We recently were in the process of migrating content from on-premise SharePoint to O365 SharePoint Online, when on a handful of sites, the owner reported a post-migration issue stating that some fields on the document were not updating when the metadata in the library was updated.
 
On analysis we found that Quick Parts had been used and we had to find a way to fix this.
In total we had around 5,000 documents across a handful of sites that were impacted by this. 
 
Solution
 
What we first found was removing the existing quick part from the word document and adding it again manually fixed it.
However, as the document count was around 5,000, doing this manually was a lot of effort and we wanted to look at other options to see if there was a way this could be scripted.
 
In our specific case, the Quick Parts in the Word document were added in the Header and Footer Sections of the document and we found that we are able to access this section using C# and Office Word API.
 
So, here is what we did to script the solution:
 
a) Create a new word document on the O365 SPO library and create the header and footer section with the Quick Part. This will serve as the reference.
b) Open this reference document using C# code, access the header the footer and save it in a variable.
c) Connect to the library that was migrated to SPO, loop through all the documents one by one, remove the header and footer sections and replace them with the value in the variable created in "b)".
d) Save the document.
 
This, in a nutshell, did the trick for us.