I needed to add a cover page to many documents and decided to automate the process with PowerShell. The real issue was to only add a cover page if one doesn't already exist.
The below script will open a given document, check if a cover page exists and add one if it doesn't
Need to detect if a cover page exists and add one if it doesn't.
I decided to create a cover page first by making a separate document that contains just the cover page I want to add and then combining it with the document that I want to add it to.
It is simple enough to create a custom cover page. Combining two documents is not too hard either. Detecting if a cover page already exists is what began to cause me issues.
I tried to see if there was any special "cover page only" attributes that got added when you used one of the OOTB templates. All of these templates just appeared to have document controls, shapes, and typical formatting. Nothing stood out initially and I couldn't find any standard method to see if one exists via code.
I then noticed that all of the OOTB templates have a content control with a label of "Title". I decided to use that as my search term so my solution outline would become:
I am assuming any cover page that may exist has a content control with a title part named "Title." If you made the cover page yourself, then you may put something else for title so you will need to compensate for that.
Even though the OOTB cover pages use such a content control, you may have even added one with the same title somewhere else in a document that doesn't actually have a cover page. Again, you will need to compensate for this as well.
How to Use:
Save the script as "WordCoverPage.ps1" and run in either one of two ways:
./WordCoverPage <full file location> <full cover page location>
powershell -command "<full script location> <full file location> <full cover page location>"
Create a ForEach loop to run this on a directory full of files.