'Excelファイルの名前定義をすべて削除する
'excel_remove_names.vbs
'引数のチェック
Set args = WScript.Arguments
If args.Count > 0 Then
file = args(0)
If Right(file,4) = ".xls" Or Right(file,5) = ".xlsx" Then
'Excelファイルは続行する
Else
Msgbox "Excelファイルではありません。", vbExclamation, "警告"
WScript.Quit
End If
Else
Msgbox "Excelファイルをvbsファイルにドラッグアンドドロップしてください。", vbInformation, "情報"
WScript.Quit
End If
'Excelオブジェクトを取得
Set excelApp = CreateObject("Excel.Application")
'ブックを開く
Set workbook = excelApp.Workbooks.Open(file)
excelApp.Visible = True
'正規表現オブジェクト
Set objRE1 = CreateObject("VBScript.RegExp")
Set objRE2 = CreateObject("VBScript.RegExp")
objRE1.Pattern = ".*!Print_Area$"
objRE2.Pattern = ".*!Print_Titles$"
'非表示の名前を表示
Dim curName
Dim cnt
cnt = 0;
For Each curName In workbook.Names
If name.Visible = False Then
name.Visible = True
cnt = cnt + 1
End If
Next
'各シートの操作
For Each curName In workbook.Names
'印刷範囲、印刷タイトルは除く
if not objRE1.Test(curName.name) and not objRE2.Test(curName.name) Then
'削除実施
On Error Resume Next
curName.Delete
On Error Goto 0
cnt = cnt + 1
end if
Next
if cnt > 0 then
'ブックを保存
workbook.Save
end if
'ブックを閉じる
workbook.Close
Set workbook = Nothing
excelApp.Quit
Msgbox "処理が完了しました。" & vbNewLine & file, vbInformation, "情報"
WScript.Quit
2020年05月26日
Excel 名前定義をすべて削除する
posted by Hiro at 19:15| Comment(0)
| プログラム