2021/10/1 追記:
本記事は公開されてから大分時間が経っており内容も古くなっております。
VBAからのブラウザー操作につきましては下記記事もご参照ください。
2017/1/31 追記:
一部コードを修正した記事を公開しました。
2019/6/5 追記:
SeleniumBasicを使ってMicrosoft Edgeを操作する方法についても記事を書きました。
Spy++でMicrosoft Edgeのウィンドウを眺めていて気が付いたことが一つ。
“下の方のウィンドウに「Internet Explorer_Server」クラスのウィンドウがある”
何故このウィンドウがあるのかは知りませんが、これがあるということは、よく使われる“ウィンドウハンドルを指定してHTMLDocumentを捕まえる方法”が使えるということ!?
さっそく試してみることにしました。
Microsoft Edgeを操作するVBAマクロ
※ 下記コードは64ビット版Officeでは動作しませんので、コードを書き換える必要があります。
'標準モジュール
Option Explicit
Private Type UUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function ObjectFromLresult Lib "oleacc" (ByVal lResult As Long, riid As Any, ByVal wParam As Long, ppvObject As Object) As Long
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const SMTO_ABORTIFHUNG = &H2
Private hEdge As Long
Private hIES As Long
Public Sub Sample()
'Microsoft EdgeをDOM操作(64ビット版Excelでは不可)
'http://www.mvps.org/emorcillo/en/code/vb6/iedom.shtml 参照
Dim msg As Long
Dim res As Long
Dim d As Object
Dim IID_IHTMLDocument As UUID
'Edge起動
CreateObject("Shell.Application").ShellExecute "microsoft-edge:http://www.yahoo.co.jp/"
Sleep 2000
EnumChildWindows 0, AddressOf EnumChildProcEdge, 0
If hEdge = 0 Then Exit Sub
EnumChildWindows hEdge, AddressOf EnumChildProcIES, 0
If hIES = 0 Then Exit Sub
msg = RegisterWindowMessage("WM_HTML_GETOBJECT")
SendMessageTimeout hIES, msg, 0, 0, SMTO_ABORTIFHUNG, 1000, res
If res Then
With IID_IHTMLDocument
.Data1 = &H626FC520
.Data2 = &HA41E
.Data3 = &H11CF
.Data4(0) = &HA7
.Data4(1) = &H31
.Data4(2) = &H0
.Data4(3) = &HA0
.Data4(4) = &HC9
.Data4(5) = &H8
.Data4(6) = &H26
.Data4(7) = &H37
End With
If ObjectFromLresult(res, IID_IHTMLDocument, 0, d) = 0 Then
d.getElementById("srchtxt").Value = "初心者備忘録"
d.getElementById("srchbtn").Click
'表示待ち
While LCase(d.readyState) <> "complete"
Sleep 100
DoEvents
Wend
MsgBox d.Title, vbInformation + vbSystemModal
End If
End If
End Sub
Private Function EnumChildProcEdge(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim buf1 As String * 255
Dim buf2 As String * 255
Dim ClassName As String
Dim WindowName As String
GetClassName hWnd, buf1, Len(buf1)
ClassName = Left(buf1, InStr(buf1, vbNullChar) - 1)
If ClassName = "ApplicationFrameWindow" Then
GetWindowText hWnd, buf2, Len(buf2)
WindowName = Left(buf2, InStr(buf2, vbNullChar) - 1)
If WindowName Like "*Microsoft Edge" Then
hEdge = hWnd
EnumChildProcEdge = False
Exit Function
End If
End If
EnumChildProcEdge = True
End Function
Private Function EnumChildProcIES(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim buf As String * 255
Dim ClassName As String
GetClassName hWnd, buf, Len(buf)
ClassName = Left(buf, InStr(buf, vbNullChar) - 1)
If ClassName = "Internet Explorer_Server" Then
hIES = hWnd
EnumChildProcIES = False
Exit Function
End If
EnumChildProcIES = True
End Function
上記コードは、 EdgeでYahoo! JAPANを開く → 「初心者備忘録」をキーワードに検索 → 検索結果のタイトルをメッセージボックスで表示、という動作を行うものですが、確認した限りでは下図のように意図した通り動作しているようです。
使い慣れたDOMでEdgeをアレコレできるのは中々便利です。
とはいえEdgeがバージョンアップすると使えなくなる可能性もあるので、実務で使う場合は、やはり公式にサポートされているWebDriverを利用した方が良いだろうと思います。
関連記事
- Microsoft Edgeを操作するVBAマクロ(WebDriver編)
- //www.ka-net.org/blog/?p=6018
- PowerShellでMicrosoft Edgeを操作する
- //www.ka-net.org/blog/?p=6029
- Microsoft Edgeを操作するVBAマクロ(DOM編)
- //www.ka-net.org/blog/?p=6033
- 続・Microsoft Edgeを操作するVBAマクロ(DOM編)
- //www.ka-net.org/blog/?p=6068
- Microsoft Edgeを操作するVBScript
- //www.ka-net.org/blog/?p=6129
- 起動中のMicrosoft EdgeからタイトルとURLを取得するVBAマクロ(UI Automation編)
- //www.ka-net.org/blog/?p=6076
- 起動中のMicrosoft EdgeからタイトルとURLを取得するVBAマクロ(DOM編)
- //www.ka-net.org/blog/?p=6086
- Microsoft EdgeでWebページを開くインターネットショートカット
- //www.ka-net.org/blog/?p=6040
- Microsoft Edgeを起動するVBScript
- //www.ka-net.org/blog/?p=6048
- Microsoft Edgeでリンク先を開く
- //www.ka-net.org/blog/?p=6050
- 「ファイル名を指定して実行」からMicrosoft Edgeを起動する
- //www.ka-net.org/blog/?p=6098





















この記事へのコメントはありません。