#はじめに
「記録文書の一覧が欲しい」
「元のWordファイルじゃなくて署名の入ったPDFファイルの一覧が欲しい」
そうかい…ファイル検索って知ってる…?
#参考資料
VBAでハイパーリンクを設定(Hyperlinks.Add)
#ソース
make_pdflist.ps1
# フォルダ内のpdfすべてのリンク付きブックを作成する
#いつものを関数化した
function FolderDialog{
[CmdletBinding()]
param(
[string]$path
)
Process{
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$process = [System.Diagnostics.Process]::GetCurrentProcess()
$window = New-Object Windows.Forms.NativeWindow
$window.AssignHandle($process.MainWindowHandle)
$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
$dialog.Description = "フォルダを選択してください"
$dialog.SelectedPath = $PWD
$ret = $dialog.ShowDialog($window)
if($ret -eq [System.Windows.Forms.DialogResult]::OK){
$Path = $dialog.SelectedPath
return $path
} else {
Read-Host "キャンセルされました。ENTERキーを押してください。"
return
}
}
}
#フォルダ選択ダイアログ関数からパスを取得
$folder = FolderDialog
$list = Get-ChildItem -Recurse $folder *.pdf
$num = $list.count
#Excelの下ごしらえ
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $true
$book = $excel.Workbooks.Add()
$sheet = $book.Sheets(1)
#内容を入力していく
1..($num) | %{
$sheet.cells.item($_,1) = $list[($_-1)].FullName
$sheet.hyperlinks.Add($sheet.cells.item($_,1),$list[($_-1)].FullName)
}
$book.SaveAs($folder +"\"+ (Split-Path $folder -Leaf)+"_pdflist.xlsx")
#末端処理
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($sheet)