(↓そんなことやるなら最初からWordで出力するな、という話)
フォルダ内にあるWordファイルから、詳細プロパティを取得するPowerShellプログラムです。詳細プロパティは、ファイルメニューからプロパティー詳細プロパティで見ることができます。
ここに使いたい情報があるけど、全てのファイルを開いて見るのは面倒だ、自動でテキストファイル出力等がしたい時にこのプログラムが使えます。
コード
$getProperties = @('myId', 'myNetwork');
$binding = "System.Reflection.BindingFlags" -as [type];
$word = New-Object -ComObject word.Application;
$word.Visible = $False;
$targetFolder = '\\QNAP\myData';
$docs = Get-ChildItem $targetFolder -Filter *.doc;
foreach($doc in $docs){
$Document = $word.Documents.Open($doc.FullName, $False, $True); #fileFullName, Convert Dialog, Read-Only
$customProperties = $Document.CustomDocumentProperties;
$getValues = @{};
foreach($prop in $getProperties){
$myProp = [System.__ComObject].InvokeMember('Item', $binding::GetProperty, $null, $customProperties, $prop);
$myVal = [System.__ComObject].InvokeMember('Value', $binding::GetProperty, $null, $myProp, $null);
$getValues.Add($prop, $myVal);
}
$Document.Close(0); #0:Don't save, -2:Prompt, -1:Save
$outputString = $doc.FullName + ">" + $getValues['myId'] + ">" + $getValues['myNetwork'];
Write-Output $outputString;
}
$word.Quit();
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$word);
[gc]::Collect();
[gc]::WaitForPendingFinalizers();
Remove-Variable word;
解説
- $getProperties … 取得したいCustomProperty名を指定する
- $Document = $word.Documents.Open($doc.FullName, $False, $True)→Excelの場合はfileを指定すればいいのですが、何故かWordはFullName指定
- $getValues … foeachで回して、CustomProperty名を取得し、値を取得(見つからない場合などエラー処理を必要に応じて追加してください)
- $Document.Close(0) … 変数に入れたので、Wordファイルは閉じてOK。0は保存しない
- $getValues[プロパティ名] … これで値が利用できます
\\QNAP\myData\a1.doc>500604>10.56.135.12 \\QNAP\myData\a2.doc>500607>10.56.145.12 \\QNAP\myData\a3.doc>500609>10.72.136.11




0 件のコメント:
コメントを投稿