Use PowerShell to Add Headers to Word Documents

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to add headers to Microsoft Word documents.

Microsoft Scripting Guy, Ed Wilson, is here. It snowed in Charlotte yesterday. It was not really a surprise because the weather service did a good job of warning that it would happen. Although the Scripting Wife and I were a bit worried that the power would go off, it turned out to be a huge non-event—at least for us. For the thousands of people who lost their power…well, that was a different story. It seems that these sorts of things always pile up—we lose the power, it is cold, there is ice and snow—it all happens at the same time. Why can’t we have a nice snowstorm during the summer when it is over 100 degrees with 98 percent humidity? Oh well.

One of the things that seems to pile on is using Windows PowerShell to automate Microsoft Word. What am I talking about? Well, about the time I am finished with one thing, I remember that there is something else I need to do. For example, if I had added my script in the static header of the Word document template that I created yesterday, I could avoid having to write that script today. The cool thing is that when I know how to automate adding a header, I can customize the header, such as to include the title of the article or some other information that would be helpful.

Begin with the Word.Application object

The place to begin is with the Word.Application object. I have to create that object as my entry point for automating Word. It is a COM object, so I also need to specify that. When I am writing a single script, I like to make the Word object visible. After I get everything working, I do not need to make the object visible, and I do that by setting the Visible property to $false. Here are those two lines of code:

$word = New-Object -comobject word.application

$word.visible = $true

I like to use the standard enumerations because it makes the script more readable. It also facilitates looking stuff up on MSDN. To do this, I create two types, which I will use directly in the script:

$HeaderFooterIndex = "microsoft.office.interop.word.WdHeaderFooterIndex" -as [type]

$alignmentTab = "microsoft.office.interop.word.WdAlignmentTabAlignment" -as [type]

To edit a Word document, I need to open it. I am working with my Template document right now, so I specify my path to the template, and I then call the Open method. A document object returns and I store it in a variable named $doc. This is shown here:

$filePath = "C:\lit\PaperTemplate.docx"

$doc = $word.documents.open($filePath)

I now need to retrieve a Section object. Keep in mind that the sections begin numbering with 1 as opposed to 0. To gain access to the Section object, I use the Item method to retrieve a specific section from the Sections collection. When I call the Sections property from the Document object, it returns a Sections collection. This collection includes the Item method. The code is shown here:

$section = $doc.sections.item(1)

Now I want to edit my first page header. To do this, I use the Headers property from the Section object to return a Headers collection. I then use the Item method to gain access to a specific header. I want the header for the first page, so I use the wdHeaderFooterFirstPage enumeration. This is a static property from the type I loaded earlier in my script.

In the Windows PowerShell ISE, I can use IntelliSense to retrieve the possible values, and therefore, to avoid having to bounce back and forth on MSDN looking stuff up. Here is that code:

$header = $section.headers.item($HeaderFooterIndex::wdHeaderFooterFirstPage)

Now, I need my header to be right-justified on my paper. So to do this, I need to use the wdRight enumeration from the WdAlignmenttabAlignment type that I added earlier. I use this value to specify that I want to insert an alignment tab. There is a method from the Range object that permits me to do this. All I do is call the method, and tell it what kind of alignment tab I want to insert. This is shown here:

$header.range.InsertAlignmentTab($alignmentTab::wdRight)

After I have inserted my alignment tab, I can finally insert my header text. To do this, I need to use the InsertAfter method from the Range object. As shown here, I simply give it a string that represents the text I want to insert:

$header.range.InsertAfter("First Page Header")

Now I need to save my changes, close the Word document, and call it quits:

$doc.save()

$doc.close()

$word.quit()

The complete script is shown here:

$filePath = "C:\lit\PaperTemplate.docx"

$HeaderFooterIndex = "microsoft.office.interop.word.WdHeaderFooterIndex" -as [type]

$alignmentTab = "microsoft.office.interop.word.WdAlignmentTabAlignment" -as [type]

$word = New-Object -comobject word.application

$word.visible = $true

$doc = $word.documents.open($filePath)

$section = $doc.sections.item(1)

$header = $section.headers.item($HeaderFooterIndex::wdHeaderFooterFirstPage)

 

$header.range.InsertAlignmentTab($alignmentTab::wdRight)

$header.range.InsertAfter("First Page Header")

$doc.save()

$doc.close()

$word.quit()

When I run the script, the Word document pops up, and then it closes. I open the file, and I see that the header was added. This is shown here:

Image of menu

That is all there is to using Windows PowerShell to add a header to a document. Word Week continues tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon