2017-05-23 2 views
0

私は特定のテキストのために多数のワードドキュメント(doc & docx)をチェックする必要があり、Scripting Guysの偉大なチュートリアルとスクリプトが見つかりました。PowershellはWord文書のヘッダーテキストを読み取らないでしょうか?

https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/01/find-all-word-documents-that-contain-a-specific-phrase/

スクリプトは、ディレクトリ内のすべてのドキュメントを読み取り、次の出力が得られます。回の

  1. 数は、特定のテキストが
  2. 特定のテキストを含むすべてのファイルのディレクトリを発見されたすべての文書で
  3. 総単語数を述べました。

これは私の必要としているコードですが、実際には特定のテキストがどこにあるかはドキュメントのヘッダーを実際に確認していないようです。すべてのヒント&スクリプトのヘッダーテキストを読むことのトリックは私を非常に幸せにするだろう。

ヘッダーテキストがドキュメントの残りの部分になるように書式を削除する方法もあります。これは可能ですか?

編集:

[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 

これは行くための正しい方法ですか、それは行き止まりのですか?

答えて

0

あなたは、ヘッダーテキストをしたい場合は、次のことを試すことができます。将来的にこの質問を見ている人のために

$document.content.Sections.First.Headers.Item(1).range.text 
+0

こんにちはミッキー、あなたの迅速な対応に感謝します! $ rangeにコードを追加しようとしましたが、次のエラーが発生しました: '[System .__ ComObject]に 'movestart'という名前のメソッドが含まれていないため、メソッドの呼び出しに失敗しました。 + $ null = $ range.movestart <<<<() Sections.First.Headers.Item(1)は$ rangeと互換性がありませんか? @Micky Balledelli –

+0

これはヘッダーの取得とは独立しています。 '$ range.MoveStart()'を使用してください。大文字と小文字が区別されます; –

+0

ありがとうミッキー、もちろん正しいです。あなたのソリューションは機能します!私はまだここでいくつかのエラーが発生しているが、彼らはヘッダービットとは何の関係もありません。 –

0

:何かはかなり上記の私のコードで作業していません。偽陽性を返して、$ pathの下に見つかったすべての文書をリストするように、文書の内容にかかわらず$ wordFound = 1を置きます。

Find.Execute内の変数を編集しても、$ wordFoundの結果は変わっていないようです。コードをステップバイステップで実行している間にエラーが発生するのは唯一の場所なので、問題は$範囲内にあると考えられます。

エラーが表示されます。

You cannot call a method on a null-valued expression. 
At C:\Users\user\Desktop\Powershell\count.ps1:24 char:58 
+ $range = $document.content.Structures.First.Headers.Item <<<< (1).range.Text 
    + CategoryInfo   : InvalidOperation: (Item:String) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

Exception calling "MoveStart" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)" 
At C:\Users\user\Desktop\Powershell\count.ps1:25 char:26 
+ $null = $range.MoveStart <<<<() 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : ComMethodCOMException 

You cannot call a method on a null-valued expression. 
At C:\Users\user\Desktop\Powershell\count.ps1:26 char:34 
+ $wordFound = $range.Find.Execute <<<< ($findText,$matchCase, 
    + CategoryInfo   : InvalidOperation: (Execute:String) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 
関連する問題