title: Powershell won't read header text in word documents?
body: <p>I am in need of checkingh a larger number of word documents (doc &amp; docx) for a specific text and found a great tutorial and script by the Scripting Guys; </p>
  
  <p><a href="https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/01/find-all-word-documents-that-contain-a-specific-phrase/" rel="nofollow noreferrer">https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/01/find-all-word-documents-that-contain-a-specific-phrase/</a></p>
  
  <p>The script reads all documents in a directory and gives the following output; </p>
  
  <ol>
  <li>Number of times mentioned</li>
  <li>Total word count in all documents where the specific text is found</li>
  <li>The directory of all files containing the specific text.</li>
  </ol>
  
  <p>This is all I need, however their code doesn't seem to actually check the headers of any document, which incidentally is where the specific text I'm looking for is located. Any tips &amp; tricks in making the script read header text would make me very happy.</p>
  
  <p>An alternative solution might be to remove the formatting so that the header text becomes part of the rest of the document? Is this possible?</p>
  
  <p>Edit: Forgot to link the script:</p>
  
  <pre><code>[cmdletBinding()]
  Param(
   $Path = "C:\Users\use\Desktop\"
  ) #end param
  
  $matchCase = $false
  $matchWholeWord = $true
  $matchWildCards = $false
  $matchSoundsLike = $false
  $matchAllWordForms = $false
  $forward = $true
  $wrap = 1
  $application = New-Object -comobject word.application
  $application.visible = $False
  $docs = Get-childitem -path $Path -Recurse -Include *.docx
  $findText = "specific text"
  $i = 1
  $totalwords = 0
  $totaldocs = 0
  
  Foreach ($doc in $docs)
  {
   Write-Progress -Activity "Processing files" -status "Processing $($doc.FullName)" -PercentComplete ($i /$docs.Count * 100) 
   $document = $application.documents.open($doc.FullName)
   $range = $document.content
   $null = $range.movestart()
   $wordFound = $range.find.execute($findText,$matchCase,
    $matchWholeWord,$matchWildCards,$matchSoundsLike,
    $matchAllWordForms,$forward,$wrap)
    if($wordFound) 
      { 
       $doc.fullname
       $document.Words.count
       $totaldocs ++
       $totalwords += $document.Words.count
      } #end if $wordFound
   $document.close()
   $i++
  } #end foreach $doc
  $application.quit()
  "There are $totaldocs and $($totalwords.tostring('N')) words"
  
  #clean up stuff
  [System.Runtime.InteropServices.Marshal]::ReleaseComObject($range) | Out-Null
  [System.Runtime.InteropServices.Marshal]::ReleaseComObject($document) | Out-Null
  [System.Runtime.InteropServices.Marshal]::ReleaseComObject($application) | Out-Null
  Remove-Variable -Name application
  [gc]::collect()
  [gc]::WaitForPendingFinalizers()
  </code></pre>
  
  <p>EDIT 2: My colleague got the idea to call on the section header instead;</p>
  
  <pre><code>Foreach ($doc in $docs)
  {
   Write-Progress -Activity "Processing files" -status "Processing $($doc.FullName)" -PercentComplete ($i /$docs.Count * 100) 
   $document = $application.documents.open($doc.FullName)
   # Load first section of the document
   $section = $doc.sections.item(1);
   # Load header
   $header = $section.headers.Item(1);
  
   # Set the range to be searched to only Header
   $range = $header.content
   $null = $range.movestart()
  
   $wordFound = $range.find.execute($findText,$matchCase,
    $matchWholeWord,$matchWildCards,$matchSoundsLike,
    $matchAllWordForms,$forward,$wrap,$Format)
    if($wordFound) [script continues as above]
  </code></pre>
  
  <p>But this is met with the following errors:</p>
  
  <pre><code>You cannot call a method on a null-valued expression.
  At C:\Users\user\Desktop\count_mod.ps1:27 char:31
  +  $section = $doc.sections.item &lt;&lt;&lt;&lt; (1);
      + CategoryInfo          : InvalidOperation: (item:String) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  You cannot call a method on a null-valued expression.
  At C:\Users\user\Desktop\count_mod.ps1:29 char:33
  +  $header = $section.headers.Item &lt;&lt;&lt;&lt; (1);
      + CategoryInfo          : InvalidOperation: (Item:String) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  You cannot call a method on a null-valued expression.
  At C:\Users\user\Desktop\count_mod.ps1:33 char:26
  +  $null = $range.movestart &lt;&lt;&lt;&lt; ()
      + CategoryInfo          : InvalidOperation: (movestart:String) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  You cannot call a method on a null-valued expression.
  At C:\Users\user\Desktop\count_mod.ps1:35 char:34
  +  $wordFound = $range.find.execute &lt;&lt;&lt;&lt; ($findText,$matchCase,
      + CategoryInfo          : InvalidOperation: (execute:String) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  </code></pre>
  
  <p>Is this the right way to go or is it a dead end?</p>