VBScript による IE オブジェクトの取得
こんにちは。
仕事の自動化にやりがいと達成感を感じるガッくんです。
この記事の目次
関数
ソースコード
この関数は URL を引数として IE オブジェクトを返す関数です。 URL が見つからなければ Nothing を返します。『VBS』というフォルダの中に『myFunc』というフォルダを作成していて、自作した関数は全て『myFunc』フォルダに保存します。
getObjectIE-targetURL.vbs
Function getObjectIE(targetURL)
'-------------------------------------------------------------------------
' IE オブジェクトの取得
'-------------------------------------------------------------------------
strMsg = ""
'-----------------------------------------------------------------------
' シェルオブジェクトの生成
'-----------------------------------------------------------------------
Set objShell = CreateObject("Shell.Application")
'-----------------------------------------------------------------------
' ウインドウコレクションのスキャン
'-----------------------------------------------------------------------
Set objIE = Nothing
For Each objWindow In objShell.Windows
'---------------------------------------------------------------------
' IWebBrowser2 だったら
'---------------------------------------------------------------------
If TypeName(objWindow) = "IWebBrowser2" Then
'-------------------------------------------------------------------
' Internet Explorer だったら
'-------------------------------------------------------------------
if objWindow.Name = "Internet Explorer" then
'-----------------------------------------------------------------
' targetURL が見つかったら
'-----------------------------------------------------------------
LookAtURL = objWindow.LocationURL
if LookAtURL = targetURL then
Set objIE = objWindow
Exit For
end if
end if
End If
Next
Set objShell = Nothing
'-------------------------------------------------------------------------
' 対象IEオブジェクトがあった場合
'-------------------------------------------------------------------------
if objIE is Nothing then
Msgbox "対象となる IE を見つけることが出来ませんでした。"
else
'Msgbox strMsg
end if
Set getObjectIE = objIE
End Function
使い方
作成した getObjectIE_targetURL.vbs ファイルを呼び出して使うプログラムを書きます。このプログラムは『VBS』フォルダの直下に保存します。
main.vbs
'------------------------------------------------------------------------------
' 自作関数のインクルード
'------------------------------------------------------------------------------
call IncludeFunction()
'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
' IE を開く
'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'--- 変数宣言 ---
targetURL = "https://transit.yahoo.co.jp/"
'--- 実行 ---
call newIE(targetURL)
'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
' IE オブジェクトを取得する
'_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
'--- 変数宣言 ---
targetURL = "https://transit.yahoo.co.jp/"
'--- 実行 ---
Set objIE = getObjectIE(targetURL)
if objIE is nothing then
'--- オブジェクトが無いので強制終了 ---
WScript.Quit
else
'--- 続行 ---
MsgBox "IE オブジェクト捕捉"
end if
'------------------------------------------------------------------------------
' 終了通知
'------------------------------------------------------------------------------
MsgBox wscript.scriptname & " の処理が完了しました。"
'------------------------------------------------------------------------------
' 自作関数をコールするサブルーチン
'------------------------------------------------------------------------------
sub IncludeFunction()
'----------------------------------------------------------------------------
' 自分(=VBSファイル)のディレクトリを取得する
'----------------------------------------------------------------------------
FullPath = wscript.scriptfullname
FileName = wscript.scriptname
pathLen= len(FullPath) - len(FileName)
crrDir = left(FullPath, pathLen)
chiDir = "myFunc"
crrDir = crrDir & chiDir
'----------------------------------------------------------------------------
' ファイルシステムを扱うオブジェクトを作成
'----------------------------------------------------------------------------
Set objFileSys = CreateObject("Scripting.FileSystemObject")
'----------------------------------------------------------------------------
' フォルダのオブジェクトを取得
'----------------------------------------------------------------------------
Set objFolder = objFileSys.GetFolder(crrDir)
'----------------------------------------------------------------------------
' FolderオブジェクトのFilesプロパティからFileオブジェクトを取得
'----------------------------------------------------------------------------
For Each objFile In objFolder.Files
'--------------------------------------------------------------------------
' 自作関数のコール
'--------------------------------------------------------------------------
FuncFile = objFile.Name
if instr(1, FuncFile, ".txt") = 0 then
Set objFile = objFileSys.OpenTextFile(crrDir & "\" & FuncFile)
ExecuteGlobal objFile.ReadAll()
objFile.Close
end if
Next
'----------------------------------------------------------------------------
' メモリ解放
'----------------------------------------------------------------------------
Set objFile = Nothing
Set objFolder = Nothing
Set objFileSys = Nothing
end sub
コメント
これで、 main.vbs を実行すると targetURL を開いている IE の オブジェクトを objIE に格納します。 targetURL が見つからない時は Nothing を objIE に格納します。
以上