################################################################################## # # # Script name: Insert-PageBreak.ps1 # Author: goude@powershell.nu # Homepage: www.powershell.nu # # ################################################################################## param ([string]$Document, [string]$Heading, [switch]$help) function GetHelp() { $HelpText = @" DESCRIPTION NAME: Insert-PageBreak.ps1 Inserts a PageBreak after Paragraphs or defined Heading types. PARAMETERS: -Document Path to Word Document (Required) -Heading Heading to add PageBreak before (Optional) -help Prints the HelpFile (Optional) SYNTAX: Insert-PageBreak.ps1 -Document C:\Documents\Word.doc Inserts a PageBreak for each Paragraph in the Document. Insert-PageBreak.ps1 -Document C:\Documents\Word.doc -Heading "Heading 1" Inserts a PageBreak for each "Heading 1" in the Word Document Insert-PageBreak.ps1 -Document C:\Documents\Word.doc -Heading "Heading 2" Inserts a PageBreak for each "Heading 2" in the Word Document Insert-PageBreak.ps1 -help Displays the help topic for the script "@ $HelpText } function Insert-PageBreak ([string]$Document, [string]$Heading) { $Word = New-Object -Com Word.Application $OpenDoc = $Word.Documents.Open($Document) $Paragraphs = $OpenDoc.Paragraphs $Paragraphs | ForEach { if($Heading) { if ($_.Style.NameLocal -match $Heading) { $_.PageBreakBefore = -1 } } else { $_.PageBreakBefore = -1 } } $OpenDoc.Close() } if ($help) { Get-Help } if ($Document -AND !$Heading) { Insert-PageBreak -Document $Document } if ($Document -AND $Heading) { Insert-PageBreak -Document $Document -Heading $Heading }