2018年02月14日

Excelファイルのユーザスタイルをすべて削除するVBScript

Excelファイルのセルのスタイルに不要なユーザスタイルが沢山ある場合に一括して削除するVBScriptです。
'Excelファイルのユーザスタイルをすべて削除する
'excel_remove_userstyle.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

'ユーザースタイルを削除する
counter = 0
For Each style In workbook.Styles
If Not style.BuiltIn Then
'削除できないStyleはエラーとなるためスキップする
On Error Resume Next
style.Delete
On Error GoTo 0
counter = counter + 1
End If
Next

'ブックを保存
If counter > 0 Then
workbook.Save
End If

'ブックを閉じる
workbook.Close
Set workbook = Nothing
excelApp.Quit

Msgbox "処理が完了しました。" & vbNewLine & file, vbInformation, "情報"
WScript.Quit
タグ:Excel VBScript
posted by Hiro at 22:26| Comment(0) | プログラム