ソース
Option Explicit
Const リミットタイム = 30
Public Enum enmプロパティIE
innerText
outerText
innerHTML
outerHTML
End Enum
Public Function Navigate(aObjIe As InternetExplorer, aURL As String, Optional aIsInvisible As Boolean) As Boolean
On Error GoTo Err
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
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
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
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
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
Public Function CloseIE(aObjIe As InternetExplorer) As Boolean
On Error GoTo Err
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
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
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
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
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
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
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
str = objIe.document.url
set obj = objIe.document
objIE.Visible = True/False
objIe.Toolbar = True/False
objIe.AddressBar = True/False
objIe.MenuBar = True/False
備考