PoshCode Archive  Artifact Content

Artifact 06cbc00a4a7a0ba7979528463a16a7d2e0c43c704a2d875731b0fda27780cfd4:

  • File Searching-Word.ps1 — part of check-in [d69d0d4338] at 2018-06-10 13:46:59 on branch trunk — From SAPIEN blog post by Jeff Hicks: http://blog.sapien.com/index.php/2008/07/28/searching-word/ (user: halr9000 size: 1331)

# encoding: ascii
# api: powershell
# title: Searching Word
# description: From SAPIEN blog post by Jeff Hicks: http://blog.sapien.com/index.php/2008/07/28/searching-word/
# version: 0.1
# type: script
# author: halr9000
# license: CC0
# x-poshcode-id: 486
# x-archived: 2016-12-06T06:29:20
# x-published: 2008-07-28T18:22:00
#
# Good example of working with styles and finding text in Word.
#
$word=New-Object -COM "Word.Application"
 
$errorlog="c:\missing.csv"
Set-Content $errorlog "Chapter,Script"
Get-ChildItem c:\test\*.doc | foreach {
    $file=$_.fullname
    Write-Host $file
    $doc=$word.Documents.Open($file) 
    
    $style=$word.ActiveDocument.Styles | 
    where {$_.namelocal -eq "code Title"}
    
    $word.Selection.Find.Style = $style
    
    while ($word.Selection.Find.Execute()) {
        $text=($word.selection.sentences | select Text).text
        $script=(Join-Path "C:\scripts\posh" $text).Trim()
 
         if ((Get-Item $script -ea "silentlycontinue").exists) {
             Write-Host "verified $script"
             Move-Item $script -destination "c:\scripts\posh\ad"
         }
        else {
            $msg="{0},{1}" -f $file,$script
            Add-Content $errorlog $msg
        }
        
     } 
     
     $doc.close()
 
 }
 
$word.quit()