'Add Headers and Footers to Word Document with Power Shell

I am looking for a way to insert headers and footers into a Microsoft Word document that was generated from within Power Shell. Is there a way to do this? If so, what is an example of some code needed to accomplish this?



Solution 1:[1]

$Document = "c:\temp\tralala.doc" # Must exist

$Word = New-Object -Com Word.Application
$Word.Visible = $true
$ExistingDoc = $Word.Documents.Open($document)
$Selection = $Word.Selection
$ExistingDoc.ActiveWindow.ActivePane.View.SeekView = 1
$Selection.TypeText("Here is my automated header")
$ExistingDoc.ActiveWindow.ActivePane.View.SeekView = 4
$Selection.TypeText("Here is my automated footer")
$ExistingDoc.Save()
$Word.Quit()

For having a list of possible values for SeekView, see here. WdSeekView section.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Brabant