【VBA】IEUtil

ソース

Option Explicit

'v1
'****************************************************
'IE操作処理
'----------------------------------------------------
'IEUtil
'----------------------------------------------------
'参照設定
'   Microsoft HTML Object Library
'   Microsoft Internet Controls
'----------------------------------------------------
'備考
'   Dim objIe As New InternetExplorer   -宣言
'****************************************************
'Navigate            -URLを開く
'ClickElementIE      -要素をクリックする
'GoBackIE            -前のページに戻る
'GoForwardIE         -次のページに進む
'RefreshIE           -画面を再表示する
'CloseIE             -IEを閉じる
'CloseIeALL          -IEをすべて閉じる
'WaitIE              -読み込み待ち
'GetElementIeByTag   -要素を取得(タグ+文字列指定)
'GetIE               -開いている画面を取得する
'GetIeByTitle        -開いている画面を取得する(タイトル名から)
'****************************************************

'定数
Const リミットタイム = 30  '最大待ち秒数を指定

'選択肢
Public Enum enmプロパティIE
    innerText
    outerText
    innerHTML
    outerHTML
End Enum

'----------------------------------------------------
'■URLを開く
'----------------------------------------------------
'引数1:対象のIEオブジェクト
'引数2:開くURL
'引数3:(Optional)画面を非表示で開く ※Trueなら非表示 デフォルトは表示
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function Navigate(aObjIe As InternetExplorer, aURL As String, Optional aIsInvisible As Boolean) As Boolean
On Error GoTo Err
    'URLを開く
    aObjIe.Navigate aURL
    '処理が終わるまで待つ
    WaitIE aObjIe
    '画面表示する/しない
    If aIsInvisible Then
        aObjIe.Visible = False
    Else
        aObjIe.Visible = True
    End If
    Navigate = True
    If logging Then AddLog "IEUtil.Navigate", "INFO ", "タイトル:" & aObjIe.document.title & " / URL:" & aURL
    Exit Function
Err:
    If logging Then AddLog "IEUtil.Navigate", "ERROR", "URL:" & aURL & " / エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■クリックして画面を開く
'----------------------------------------------------
'備考:引数3ありの場合、新規ウインドウで開く
'     引数3なしの場合、既存ウインドウで開く
'----------------------------------------------------
'引数1:対象のIEオブジェクト
'引数2:クリックする要素
'引数3:(Optional)新規ウィンドウの格納先オブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function ClickElementIE(aObjIe As InternetExplorer, aElement As Object, Optional aReturn As InternetExplorer) As Boolean
On Error GoTo Err
    Dim strOuterHTML As String: strOuterHTML = aElement.outerHTML
    '新規で開くか既存で開くか
    aElement.target = IIf(aReturn Is Nothing, "_self", "_blank")
    'クリック
    aElement.Click
    '(新規で開かない場合)処理を待つ
    If aReturn Is Nothing Then
        WaitIE aObjIe
    '(新規で開く場合)オブジェクト取得して返す
    Else
        Set aReturn = GetIE
    End If
    ClickElementIE = True
    If logging Then AddLog "IEUtil.ClickElement", "INFO ", "要素:" & strOuterHTML
    Exit Function
Err:
    If logging Then AddLog "IEUtil.ClickElement", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■前のページに戻る
'----------------------------------------------------
'引数:対象のIEオブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function GoBackIE(aObjIe As InternetExplorer) As Boolean
On Error GoTo Err
    '前のページに戻る
    aObjIe.GoBack
    '処理が終わるまで待つ
    WaitIE aObjIe
    GoBackIE = True
    If logging Then AddLog "IEUtil.GoBackIE", "INFO ", "タイトル:" & aObjIe.document.title & " / URL:" & aObjIe.document.url
    Exit Function
Err:
    If logging Then AddLog "IEUtil.GoBackIE", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■次のページに進む
'----------------------------------------------------
'引数:対象のIEオブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function GoForwardIE(aObjIe As InternetExplorer) As Boolean
On Error GoTo Err
    '次のページに進む
    aObjIe.GoForward
    '処理が終わるまで待つ
    WaitIE aObjIe
    GoForwardIE = True
    If logging Then AddLog "IEUtil.GoForwardIE", "INFO ", "タイトル:" & aObjIe.document.title & " / URL:" & aObjIe.document.url
    Exit Function
Err:
    If logging Then AddLog "IEUtil.GoForwardIE", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■画面を再表示する
'----------------------------------------------------
'引数:対象のIEオブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function RefreshIE(aObjIe As InternetExplorer) As Boolean
On Error GoTo Err
    '再表示
    aObjIe.Refresh
    '処理が終わるまで待つ
    WaitIE aObjIe
    RefreshIE = True
    If logging Then AddLog "IEUtil.RefreshIE", "INFO ", "タイトル:" & aObjIe.document.title & " / URL:" & aObjIe.document.url
    Exit Function
Err:
    If logging Then AddLog "IEUtil.RefreshIE", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■IEを閉じる
'----------------------------------------------------
'引数:対象のIEオブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function CloseIE(aObjIe As InternetExplorer) As Boolean
On Error GoTo Err
    'タイトルとURLを保持
    Dim url As String: url = aObjIe.document.url
    Dim title As String: title = aObjIe.document.title
    '閉じる
    aObjIe.Quit
    CloseIE = True
    If logging Then AddLog "IEUtil.CloseIE", "INFO ", "タイトル:" & title & " / URL:" & url
    Exit Function
Err:
    If logging Then AddLog "IEUtil.CloseIE", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■IEをすべて閉じる
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function CloseIeALL() As Boolean
On Error GoTo Err
    Dim i As Long
    Dim win As Object
    Dim objShell As Object: Set objShell = CreateObject("Shell.Application")
    
    '起動中のウィンドウを順番にみていく
    For i = objShell.Windows.Count - 1 To 0 Step -1
        If objShell.Windows((i)).Name = "Internet Explorer" Then objShell.Windows((i)).Quit
    Next
    CloseIeALL = True
    If logging Then AddLog "IEUtil.CloseIeALL", "INFO "
    GoTo Finally
Err:
    If logging Then AddLog "IEUtil.CloseIeALL", "ERROR", "エラー内容:" & Err.Description
Finally:
    Set objShell = Nothing
End Function

'----------------------------------------------------
'■読み込み待ち
'----------------------------------------------------
'引数:対象のIEオブジェクト
'----------------------------------------------------
Public Function WaitIE(aObjIe As InternetExplorer)
    'リミット時間取得
    Dim limitTime As Date: limitTime = DateAdd("S", リミットタイム, Now())
    '完全にページが表示されるまで待機する
    Do While aObjIe.Busy = True Or aObjIe.ReadyState <> 4
        DoEvents
        'リミットまで待っても開かなければ終了
        If Now() > limitTime Then
            Exit Function
        End If
    Loop
End Function

'----------------------------------------------------
'■要素を取得(タグから+文字列指定)
'----------------------------------------------------
'引数1:対象のIEオブジェクト
'引数2:要素名
'引数3:検索文字
'引数4:(Optional)検索する箇所 ※デフォルトはouterHTML
'----------------------------------------------------
'戻り値:取得結果
'----------------------------------------------------
Public Function GetElementIeByTag(aObjIe As InternetExplorer, aElementName As String, aString As String, Optional aProperty As enmプロパティIE = outerHTML) As Object
    Dim objTag As Object
    Select Case aProperty
        Case enmプロパティIE.outerHTML
            For Each objTag In aObjIe.document.getElementsByTagName(aElementName)
                If InStr(objTag.outerHTML, aString) > 0 Then
                    Set GetElementIeByTag = objTag
                    Exit Function
                End If
            Next
        Case enmプロパティIE.innerHTML
            For Each objTag In aObjIe.document.getElementsByTagName(aElementName)
                If InStr(objTag.innerHTML, aString) > 0 Then
                    Set GetElementIeByTag = objTag
                    Exit Function
                End If
            Next
        Case enmプロパティIE.outerText
            For Each objTag In aObjIe.document.getElementsByTagName(aElementName)
                If InStr(objTag.outerText, aString) > 0 Then
                    Set GetElementIeByTag = objTag
                    Exit Function
                End If
            Next
        Case enmプロパティIE.innerText
            For Each objTag In aObjIe.document.getElementsByTagName(aElementName)
                If InStr(objTag.innerText, aString) > 0 Then
                    Set GetElementIeByTag = objTag
                    Exit Function
                End If
            Next
    End Select
End Function

'----------------------------------------------------
'■開いている画面を取得する
'----------------------------------------------------
'戻り値:取得結果
'----------------------------------------------------
Public Function GetIE() As Object
    Dim objShell As Object: Set objShell = CreateObject("Shell.Application")
    Set GetIE = objShell.Windows(objShell.Windows.Count - 1)
    WaitIE GetIE
End Function

'----------------------------------------------------
'■開いている画面を取得する(タイトル名から)
'----------------------------------------------------
'引数1:タイトル名
'引数2:(Optional)部分一致 ※Trueで部分一致 デフォルトは完全一致
'----------------------------------------------------
'戻り値:取得結果
'----------------------------------------------------
Public Function GetIeForTitle(aTitle As String, Optional aIsPartialMatch As Boolean) As Object
    Dim win As Object
    Dim objShell As Object: Set objShell = CreateObject("Shell.Application")
    
    '起動中のウィンドウを順番にみていく
    For Each win In objShell.Windows
        'IEとエクスプローラがシェルで取得されるため、IEのみ処理
        If TypeName(win.document) = "HTMLDocument" Then
            If win.document.title = aTitle Then
                Set GetIeForTitle = win
                Exit Function
            End If
        End If
    Next
End Function

機能

    '宣言
    Dim objIe As New InternetExplorer

    '要素を取得 ※添え字は0から
    Set obj = objIe.document.getElementById("id属性名")
    Set obj = objIe.document.getElementsByName("name属性名")(0)
    Set obj = objIe.document.getElementsByClassName("class属性名")(0)
    Set obj = objIe.document.getElementsByTagName("tag属性名")(0)
    Set obj = objIe.document.querySelector("querySelector")

    'タイトル
    str = objIe.document.title
    'URL
    str = objIe.document.url
    'HTMLDocument
    set obj = objIe.document
    
    '画面の表示、非表示
    objIE.Visible = True/False
    'ツールバーの表示、非表示
    objIe.Toolbar = True/False
    'アドレスバーの表示、非表示
    objIe.AddressBar = True/False
    'メニューバーの表示、非表示
    objIe.MenuBar = True/False

備考

f:id:vist764:20210820141554p:plain:w400