2
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ExcelVBAで、IE(あるいはIEモードのEdge)を捕まえて、(html)documentの中身をさらう

Last updated at Posted at 2021-09-08

#ExcelVBAで、IE(あるいはIEモードのEdge)を捕まえて、(html)documentの中身をさらう

###エクセルファイル
santarou6/excelvba_ie_edge
https://github.com/santarou6/excelvba_ie_edge/blob/main/sample.xlsm

##従来方式

sample.vb

'/----------
Sub a1()
    Set objShell = CreateObject("Shell.Application")
    For Each ie_target In objShell.Windows
        Debug.Print ie_target.LocationName
    Next
End Sub

'/----------
Sub a2()
    Dim ie_target
    Set objShell = CreateObject("Shell.Application")
    
    For Each ie_target In objShell.Windows
       'Debug.Print ie_target.LocationName
    
        pp = "Google" '"捕まえたいIEのウィンドウのタイトル名"
    
        If Left(ie_target.LocationName, Len(pp)) = pp Then

        Set ie_object = ie_target

        For Each i In ie_object.document.all
            If i.tagName = "DIV" Then
                Debug.Print i.innerHTML
            End If
        Next i
        
        End If
    
    Next
End Sub

##「IE11」終了後、EdgeのIEモードの場合

下記で、Internet Explorer_Serverから、
HtmlDocumentを取得するソースをgithubに上げてらっしゃいました。
素晴らしい。。。


Benshi/HtmlDocumentHelper.bas
https://gist.github.com/Benshi/8442005d21e8e74bd4d3735a3e77c417

https://twitter.com/Benshi_Orator/status/1395608086807650304
https://twitter.com/Benshi_Orator/status/1396767727314903040
https://twitter.com/Benshi_Orator/status/1396770128012791818


##呼び出し例(エクセル)

HtmlDocumentHelper.bas の 呼び出し例(エクセル)

sample.vb

Sub test1()

    Dim hd As MSHTML.IHTMLDocument
    Set hd = GetHtmlDocument(&H95065C) '← 95065C が hwnd
    Debug.Print hd.body.outerHTML
    Sheets("Sheet1").Cells(1, 1).Value = hd.body.outerHTML

End Sub

##IEのhwndを得る方法

Internet Explorer_Serverのhwndを得る方法は、以下が一例

ExcelVBAで、Windowsの全画面のhwndとClassNameを列挙(一部クラス名で絞込)
https://qiita.com/santarou6/items/6a2c2e186cd0d555d2bd

###エクセルファイル
santarou6/excelvba_ie_edge
https://github.com/santarou6/excelvba_ie_edge/blob/main/sample.xlsm

以上

2
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?