Add Custom Headers to Folder Full of Word Documents


Summary: Microsoft Scripting Guy, Ed Wilson, talks about reading a CSV file and adding custom headers to a folder full of Word documents.

Microsoft Scripting Guy, Ed Wilson, is here. The snow continues to befuddle the drivers around here, and as a result, the Scripting Wife and I are staying home. This has given me a lot of time to sit, look at the giant slip-and-slide outside (also called a road), and write Windows PowerShell scripts. When I combine Word and Windows PowerShell, it is guaranteed to produce a lot of fun.

Begin with my CSV file

The other day, I created a CSV file with the titles of various plays that will be covered in a Shakespeare class. I then copied a research paper template, and I created all of the document files that will be required for the class. Yesterday I talked about using Windows PowerShell to add a header to a document file. Today, I add an additional column to the CSV file, and go through it to add the headers to the document files. This illustrates the power of using Windows PowerShell, because I can add custom headers—that is, the title of each research paper as the header for the file. Here is the CSV file:

Image of text

Note  Today’s script builds off of yesterday's script. You should refer to yesterday’s blog post, Use PowerShell to Add Headers to Word Documents, for an explanation of that script.

The first thing I do is specify the path to the folder that contains the various Word documents. I then import the CSV file by using the Import-CSV cmdlet. Because the file does not have a CSV file extension, it does not mean that Windows PowerShell does not know how to properly import it. This code is shown here:

$filePath = "C:\lit"

$topics = import-csv -Path C:\lit\Shakespeare.txt

Now I create my two enumerations, the Word.Application object, and I set the application to be invisible. This script was discussed yesterday, and it is shown here:

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

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

$word = New-Object -comobject word.application

$word.visible = $false

I gather a collection of file objects by using the Get-ChildItem cmdlet to read the location that contains my Word documents. I use the FullName property to specify the file name and the path to the file. As shown here, I use Foreach to walk through the collection of file objects:

Foreach ($file in Get-ChildItem $filepath)

{

 $doc = $word.documents.open($file.FullName)

It is time to obtain my header object and to right-align the header text. This is the same code that I used yesterday:

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

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

I need a way to find the appropriate topic title that I want to add to my header. Because I already imported my CSV file, I did not want to cycle through it. I hit on the idea of using the Where method in Windows PowerShell 4.0, which is added to objects so that I could find the specific play. Then I return the topic associated with that play. The rest of the script is the same as I used yesterday:

$header.range.InsertAfter($($topics.where({$file.BaseName -match $PSItem.play}).topic))

I save and close the document file. I then close the script block from the Foreach:

$doc.save()

 $doc.close() }

The last thing to do is quit the Word.Application object:

  $word.quit()

The complete script is shown here:

$filePath = "C:\lit"

$topics = import-csv -Path C:\lit\Shakespeare.txt

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

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

$word = New-Object -comobject word.application

$word.visible = $false

Foreach ($file in Get-ChildItem $filepath)

{

 $doc = $word.documents.open($file.FullName)

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

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

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

 $header.range.InsertAfter($($topics.where({$file.BaseName -match $PSItem.play}).topic))

 $doc.save()

 $doc.close() }

 

 $word.quit()

Here is one of my newly modified Word documents:

Image of text

That is all there is to using Windows PowerShell to add custom headers to Word documents. Join me tomorrow when I will talk about more fun Windows PowerShell 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

Comments (0)

Skip to main content