PoshBytes: Fixing Word Formatting with PowerShell
Three blank pages at the end of a Word document that refuse to disappear. Instead of fighting Word’s formatting ghosts, we use PowerShell to surgically fix everything.
This post is a companion for the video embedded below. Scroll down to see the code from the video.
Example of something
Open Word and the document
$Word = New-Object -ComObject Word.Application
$Word.Visible = $true
$Doc = $Word.Documents.Open(".\MyWordDocument.docx")
Loop through each paragraph
foreach($Paragraph in $Doc.Paragraphs) {
"--new---"
$Paragraph.Range.Text
}
Once we hit the last real paragraph, delete everything after it
$kill = $false
foreach($Paragraph in $Doc.Paragraphs) {
if($kill) {
$Paragraph.Range.Delete()
}
elseif ($Paragraph.Range.Text -match "Someone who cursed the entire unit") {
$kill = $true
}
}
Save and clean up
$Doc.Save()
$Doc.Close()
$Word.Quit()
Wrap Up
• Word formatting can create blank pages that refuse to delete
• PowerShell can automate Word through the COM object
• Loop through $Doc.Paragraphs to inspect document content
• Use a flag to delete everything after the last real paragraph
• Automation beats fighting formatting every time