私は特定のテキストのために多数のワードドキュメント(doc & docx)をチェックする必要があり、Scripting Guysの偉大なチュートリアルとスクリプトが見つかりました。PowershellはWord文書のヘッダーテキストを読み取らないでしょうか?
スクリプトは、ディレクトリ内のすべてのドキュメントを読み取り、次の出力が得られます。回の
- 数は、特定のテキストが
- 特定のテキストを含むすべてのファイルのディレクトリを発見されたすべての文書で
- 総単語数を述べました。
これは私の必要としているコードですが、実際には特定のテキストがどこにあるかはドキュメントのヘッダーを実際に確認していないようです。すべてのヒント&スクリプトのヘッダーテキストを読むことのトリックは私を非常に幸せにするだろう。
ヘッダーテキストがドキュメントの残りの部分になるように書式を削除する方法もあります。これは可能ですか?
編集:
[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()
EDIT 2:スクリプトをリンクするために忘れてしまった私の同僚ではなく、セクションヘッダに呼び出すためのアイデアを得ました。
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]
しかし、これは、次のエラーで満たされている:
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 <<<< (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 <<<< (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 <<<<()
+ 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 <<<< ($findText,$matchCase,
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
これは行くための正しい方法ですか、それは行き止まりのですか?
こんにちはミッキー、あなたの迅速な対応に感謝します! $ rangeにコードを追加しようとしましたが、次のエラーが発生しました: '[System .__ ComObject]に 'movestart'という名前のメソッドが含まれていないため、メソッドの呼び出しに失敗しました。 + $ null = $ range.movestart <<<<() Sections.First.Headers.Item(1)は$ rangeと互換性がありませんか? @Micky Balledelli –
これはヘッダーの取得とは独立しています。 '$ range.MoveStart()'を使用してください。大文字と小文字が区別されます; –
ありがとうミッキー、もちろん正しいです。あなたのソリューションは機能します!私はまだここでいくつかのエラーが発生しているが、彼らはヘッダービットとは何の関係もありません。 –