連携 WindowsScriptHostを使いVBやVBA制御
※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。
※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。
WindowsScriptHost
- WSH(Windows Script Host)-VBS-スクリプト言語
- WSHはスクリプト言語VBScriptやJScriptを利用可能
- COMオブジェクト(Windows機能)を制御可能
- VBScriptの拡張子[.vbs]
- JScriptの拡張子[.js]
- [.wsf]利用可能
- 記述編集にはテキスト・エディタを使用する又は
- Office付属するMicrosoft Script Editorを使用する
- 実行はダブルクリックで実行可能
- スクリプト・ホストは以下の2種類
- wscript.exe(WScript)入力-ダイアログ・ボックス、出力-メッセージ・ボックスつまり「GUIベース」
- script.exe(CScript)入力-コマンド・プロンプト、出力-コマンド・プロンプトつまり「コンソール・ベース」

test.vbs内容記述
'// 全ドライブのごみ箱を空にする。
Set shell = WScript.CreateObject( "Shell.Application" )
For Each DesktopFolder In shell.NameSpace( 0 ).Items
If DesktopFolder.Name = "ごみ箱" Then
DesktopFolder.InvokeVerb "ごみ箱を空にする(&B)"
WScript.Quit 0
End If
Next

上のtest.vbsを実行すると
test.js内容記述
// メモ帳を起動する
// 変数定義
exec = "notepad.exe";
// 検索
var shell = WScript.CreateObject( "WScript.Shell" );
shell.Run( exec );

下のtest.jsを実行すると

Microsoft Script Editorでも編集可能

実際のMicrosoft Script Editor画面

TestVBS.vbsを実行する
TestVBS.vbs内容記述
'// エクセルVBAマクロを実行する
set obj = CreateObject("Excel.Application")
obj.Workbooks.Open("C:\Temp\test.xls")
obj.Visible = true
obj.run "TestSub"
test.xlsのTestSub内容記述
Sub TestSub()
MsgBox "成功!"
End Sub

