WindowsアプリからWScript.exeのWScriptオブジェクトを利用する。
VB6、EXCEL VBA、IE、HTA、WScriptなどのWindowsアプリから、
WScript.exeを起こして、WindowsアプリからWScript.Sleep()や
WScript.CreateObject()を使い、
WScript.ShellのPopUp()の時間指定を使います。
WScript.VBS
WScript.exeのWScriptオブジェクト(IHost_Class)を提供する部品側のサンプルです。
Set Shell=CreateObject("Shell.Application")
For Each ie In Shell.Windows()
If ie.hwnd=CLng(WScript.Arguments.Item(0)) Then Exit For
Next
ie.PutProperty "WScript",WScript
Do While TypeName(ie)="IWebBrowser2"
WScript.Sleep 1000
Loop
MsgBox "Ended."&vbTab,vbInformation,WScript.ScriptName
[説明]
Windowアプリ側に渡すため、WScriptオブジェクトをIEに渡します。
受け渡し用のIEが存在する間、待ち合わせをします。
Windowアプリ側が終了すると、IEが終了するので、抜けます。
一応動作確認用に終了のMsgBoxを出していますが、動作確認が済めば不要です。
GetWScript.VBS
WScript.exeを利用するWindowsアプリ側のサンプルです。
VBSファイルですが、VB/VBAのコードに貼り付けても使えます。
Set ie = CreateObject("InternetExplorer.Application")
Set wShell = CreateObject("WScript.Shell")
wShell.Run "WScript.exe ""WScript.VBS"" " & ie.Hwnd
For k = 1 To 10
If Not IsEmpty(ie.GetProperty("WScript")) Then
Set W = ie.GetProperty("WScript")
If TypeName(W) = "IHost_Class" Then Exit For
End If
' WScript.Sleep 100
' Application.Wait Now + TimeSerial(0, 0, 1)
wShell.Run "ping localhost -n 2",0,True
Next
Set wwShell = W.CreateObject("WScript.Shell")
wwShell.Popup TypeName(W) & vbTab, 5, "Popup 5秒で閉じます", vbInformation
t1=Timer
W.Sleep 100
MsgBox (Timer-t1)*1000, vbInformation, "Sleep 100 mSecの実時間"
If vbYes = MsgBox("WScript.Quit ?" & vbTab, vbYesNo + vbQuestion, "GetWScript") Then W.Quit
MsgBox "Exit OK ?" & vbTab & vbTab, vbQuestion, "GetWScript"
'ie.Quit
[説明]
WScriptオブジェクトを受け渡しするために、IEを利用します。
IEはウィンドウハンドルで識別します。
WScript.exe側を起動します。
WSH以外での利用を想定してWScript.Sleepをpingで代替しています。
WScriptオブジェクトをここでは変数[W]で受け取ります。
[WScript].CreateObject()でWScript.Shellオブジェクトを作成します。
このWScript.ShellオブジェクトのPopUp()では時間指定が有効です。
[WScript].Sleep()が使えます。
[WScript].Quit
すると、WScript.exeはすぐ終了します。
Windowsアプリ側が終了してIEの参照を解放するか、IEを終了すると、
それをWScript.exe側が検知して、終了します。
« すべてのウィンドウを元のサイズに戻す。その3 | トップページ | IEで、Web ページを、そのファイル名で保存する。 »