概要
パスワード付きのExcelファイルたくさん(パスワードはすべて共通)をいっぺんに開きたいので、Poweshellスクリプトを書いた。
使い方
- 開きたいファイルを適当なフォルダにまとめておく。
- 同じフォルダに以下のスクリプトを
open_excels.ps1
として保存(メモ帳を開きスクリプトをコピペ、open_excels.ps1
と名前を付けて保存)。 - エクスプローラで
open_excel.ps1
を右クリック、「Powershellで実行」を選択 - (「実行ポリシーを変更しますか?」などの警告が表示された場合)y + Enter
- パスワードを入力しEnter
- ファイルが順番に自動で開く(開けなかった場合、「開けませんでした」と表示)
- Enterを押して終了
スクリプト
open_excel.ps1
$password = Read-Host("パスワードを入力")
$filepaths = Get-Item *.xlsx
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $true
foreach($filepath in $filepaths){
try {
Write-Output $filepath
$excel.Workbooks.Open($filepath, [Type]::Missing, [Type]::Missing, [Type]::Missing, $password) > $null
} catch {
Write-Host "ファイル" $filepath "は開けませんでした"
}
}
$excel = $null
[System.GC]::Collect()
Read-Host "Enterで終了"