Powershell - counting characters in Word documents - including headers and footers

Viewed 122

I need to count the number of characters in a word document including the characters in the headers and footers. I can successfully count the number of chars in the main document, thanks to the help I got here : Powershell How to get character count at word document? but I have not succeeded in finding how to get to the headers and footers. Any ideas ? Thanks a lot in advance for any help.

EDIT :

following code loops through the headers and footers of a word document but I can not seem to be able to get their sizes.

Add-Type -AssemblyName System.Windows.Forms
$browser = New-Object System.Windows.Forms.FolderBrowserDialog
$null = $browser.ShowDialog()
$path = $browser.SelectedPath

Get-ChildItem $path -Recurse -File | 
Foreach-Object {
$Word = New-Object -comobject Word.Application
$filename = $_.FullName
$Word.Visible = $False
$datasheet = $word.Documents.Open($filename,$false, $true)

$Word.ActiveDocument.Content.Select()
 

foreach ($section in $datasheet.Sections)
{
    ForEach ($header in $section.Headers)
    {
        #$header.Range.Fields.Update() | Out-Null
        $NbHeader = $header.Range.Text.Count.ToString()
        "header $NbHeader"
    }
    ForEach ($footer in $section.Footers)
    {
        #$footer.Range.Fields.Update() | Out-Null
        $NbFooter = $footer.Range.Text.Count.ToString()
        "footer $NbFooter"
    }
}


$datasheet.close()
$word.Quit()
}
Read-Host -Prompt "Press Enter to exit"
1 Answers

You can access both the Headers and the Footers from the Document.Sections property. There are different headers/footers for primary, first page, and even pages so we loop through each with ForEach-Object (shown below with alias '%'), trim any whitespace off the end and filter out any that are only whitespace.

$word = New-Object -ComObject 'Word.Application'
$doc = $word.Documents.Open('C:\temp\word_count.docx', $null, $true)

& {
    $doc.Paragraphs | % Range | % Text | % TrimEnd

    $doc.Sections | % Headers | % Range | % Paragraphs | % Range | % Text | % TrimEnd | ? {-not [string]::IsNullOrWhiteSpace($_)} 

    $doc.Sections | % Footers | % Range | % Paragraphs | % Range | % Text | % TrimEnd | ? {-not [string]::IsNullOrWhiteSpace($_)} 

} | Measure-Object length -Sum | ForEach-Object Sum

$doc.Close()
$word.Quit()

To test and show what is being counted try this

$word = New-Object -ComObject 'Word.Application'
$doc = $word.Documents.Open('C:\temp\wordcount.docx', $null, $true)

& {
    $doc.Paragraphs | % Range | % Text | % TrimEnd

    $doc.Sections | % Headers | % Range | % Paragraphs | % Range | % Text | % TrimEnd | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } 

    $doc.Sections | % Footers | % Range | % Paragraphs | % Range | % Text | % TrimEnd | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } 

} | ForEach-Object -Begin {$tally = 0} {
    [PSCustomObject]@{
        Length = $_.length
        Tally = $tally += $_.length
        Text = $_
    } 
} 

$doc.Close()
$word.Quit()
Related