Insert a Page Break in a Word Document using PowerShell

I got this idea from “dmtelf” who posted a comment on a previous post I wrote regarding Microsoft Word and PowerShell. I ran through a few tests and this is the result.

First, lets Insert a Page Break after every Paragraph in a Word Document. The script will insert a Page break after each Paragraph. I’ve created a sample Word Document that has 5 Lorem Ipsum paragraphs.

word-03

Connecting to the Word Document is done through the Word.Application -Com Object. Then we use the Open() method to connect to our Word Document. Next we create a New Variable holding every Paragraph in the Word Document, Loop through the Paragraphs and set the PageBreak to -1. This sets a Pagebreak for each new Paragraph.


function Insert-PageBreak ([string]$File) {
	$Word = New-Object -Com Word.Application
	$OpenDoc = $Word.Documents.Open($File)
	$Paragraphs = $OpenDoc.Paragraphs

	$Paragraphs | ForEach {

		$_.PageBreakBefore = -1

	}

	$OpenDoc.Close()
}

Running the Function on a Word Document inserts a PageBreak after each Paragraph as shown below:


PS > Insert-PageBreak "C:\Documents\Lorem Ipsum.doc"

word-04

Now, what if i want to insert a Pagebreak after a “Heading 1″ instead of a paragraph, when I write Word documents i sometimes get a little sloppy and push “enter” a little too often so that i get lots of unwanted paragraphs.

word-05

We can solve this with a simple if statement as shown below.


function Insert-PageBreak ([string]$File) {
	$Word = New-Object -Com Word.Application
	$OpenDoc = $Word.Documents.Open($File)
	$Paragraphs = $OpenDoc.Paragraphs

	$Paragraphs | ForEach {

	$_.Style.NameLocal
		if ($_.Style.NameLocal -match "Heading 1") {
			$_.PageBreakBefore = -1
		}
	}

	$OpenDoc.Close()
}

Running the Function on a Word Document containing “Heading 1″ sets the PageBreak for each New “Heading 1″


PS > Insert-PageBreak "C:\Documents\Lorem Ipsum.doc"

word-06

Note that if the Headings are not correctly inserted in the Word Document, it might screw things up a little in the Document, so take a backup before trying this at home :)

Click Here to download a Script with a little more functionality

Rating 3.00 out of 5
[?]

2 thoughts on “Insert a Page Break in a Word Document using PowerShell

  1. Hi,

    I am getting the following error, when I tried to replace a string using the above code. I am trying a replace a variable with value ( which has around 20 lines). Any suggestion. Could you please let me what is the above code ?(.Net or VBA etc)

    PS C:\> C:\local\Powershell\File1.ps1
    Exception calling “Execute” with “11″ argument(s): “String parameter too long.”
    At C:\local\Powershell\File1.ps1:24 char:25
    + $Selection.Find.Execute <<<

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Protection by WP-SpamFree