Excel
VBScript

VBScriptについて


VBSでEXCELマクロ実行

pushd E:\test

RunMacro.vbs マクロ.xlsm

--

RunMacro.vbs (macro1はEXCELマクロの名前)

Dim BookPath

Dim FS, Folder
Dim Excel, Book, Sheet

main

If Err.Number = 0 Then
WScript.Quit(0)
Else
MsgBox Err.Description

If IsNull(Excel) Then
Excel.Quit
End If

WScript.Quit(1)
End If

Sub main()

BookPath = WScript.Arguments(0)

Set FS = WScript.CreateObject("Scripting.FileSystemObject")
Set Folder = FS.GetFolder(".")

If Mid(BookPath, 1, 1) <> "\" And Mid(BookPath, 2, 1) <> ":" Then
BookPath = Folder.path + "\" + BookPath
End If

Set Excel = WScript.CreateObject("Excel.Application")

Excel.Visible = true
Excel.DisplayAlerts = true

Excel.Workbooks.Open BookPath

Set Book = Excel.Workbooks(1)
Set Sheet = Book.Sheets(1)

Excel.Run "macro1"
'①②③はどちらでもマクロファイルを保存なしで終了する
'Book.Saved=true
'②
'Book.Close False
'③
Excel.DisplayAlerts = False
'マクロファイルを保存して終了する
'Book.Save
'Book.Close
Excel.Quit

End Sub