食っちゃ寝システムができるまで

「食っちゃ寝システム」ができるまでの、棚卸&備忘録です。

VBA使用頻度の高いコード(16):インターネットエクスプローラー上で開いているページを調べるコード

f:id:taikobox:20181209173448p:plain

インターネットエクスプローラー上で開いているページを調べるコード

開いているインターネットエクスプローラーが指定されたページを開いているか確認するコード


===============================

Sub ImportIE()

Dim shl As Object 'シェルオブジェクト生成
Dim htmlDoc As HTMLDocument
Dim win As Object, getFlag, txtFlag, PageFlag As Boolean
Dim targetTitle As String 'タイトル確認


Set shl = CreateObject("Shell.Application")
targetTitle = "特定するページのタイトル"

For Each win In shl.Windows '起動中のウィンドウを順番確認

'IEエクスプローラがシェルで取得されるため、IEのみ処理
If TypeName(win.document) = "HTMLDocument" Then
If win.document.Title = targetTitle Then

Dim objIE As New InternetExplorer
Set objIE = win

getFlag = True '正しく取得できた
Exit For
End If
End If

Next

If getFlag = False Then
MsgBox "目的のWebページが開かれていません。⇒IE開きます", vbExclamation
Call IEOpen
Exit Sub
End If

Next i

Set shl = Nothing
Set win = Nothing
Set htmlDoc = Nothing


End Sub

===============================

Sub IEOpen()
Dim IE As Object

target = "https://www.wave-s.jp/wave/"
Set IE = CreateObject("InternetExplorer.Application")
With IE
'(1)表示
'InternetExplorerを表示
.Visible = True

'指定したURLのページを表示する
.navigate target

'完全にページが表示されるまで待機する
Do While .Busy = True Or .readyState <> 4
DoEvents
Loop

'完全にドキュメントが読み込まれるまで待機する
Do While .document.readyState <> "complete"
DoEvents
Loop
End With

End Sub

===============================