VBA Program to get a IE HTML DOM Tree (1 Viewer)

gypsyjoe11

Registered User.
Local time
Today, 12:43
Joined
Feb 19, 2010
Messages
46
Hi I made a program that people might want to use. It recursively gets all the tags in a HTML document.

Code:
Public Sub SearchDom(myFrame As HTMLDocument, myFile As String, Optional mySpace As String = "")
 
    On Error Resume Next
    Open myFile For Append As #1
 
    For i = 0 To myFrame.all.Length - 1
        Print #1, mySpace & myFrame.all(i).tagName
        SearchDom myFrame.all(i), myFile, mySpace & "   "
    Next
 
End Sub


Below is the another program that checks all open IE browsers for the right one and then calls the above subroutine.

Code:
Public Sub testFind()
 
  'find the right internet explorer webpage
    Dim allExplorerWindows As New SHDocVw.ShellWindows
    Set allExplorerWindows = New SHDocVw.ShellWindows
 
    Dim IEwindow As SHDocVw.InternetExplorer
    Dim foundFlag As Boolean
    foundFlag = False
 
    For Each IEwindow In allExplorerWindows
        If InStr(IEwindow.LocationURL, ".direct-access.us") <> 0 Then
            foundFlag = True
            Exit For    'found the right IE window URL
        End If
    Next
 
    If Not foundFlag Then
        Dim DisplayBox As VbMsgBoxResult
        DisplayBox = MsgBox("Could not find an open instance running.", vbOKOnly, "Error!")
        Exit Sub
    End If
  'end find
 
    'get the pages entire main IE document
    Dim mainDoc As HTMLDocument
    Set mainDoc = IEwindow.document
 
    SearchDom mainDoc, "Z:\JWClifford\Home\myHTML.txt"
 
End Sub

People could modify it to print out the innerHTML or outerHTML or search for text. *Note - You have to press the stop(reset) button in your VB editor in order for it to finish making the text file. It seems to hold some in the buffer until you press stop. I can't close the file inside the routine because it uses recursion.
 

iworkonline

Registered User.
Local time
Today, 09:43
Joined
May 25, 2010
Messages
44
Dim mainDoc As HTMLDocument

Compile Error: User-Defined Type not definded

This is the error I got when I tried to use your code. Can you please tell me how to define HTMLDocument. Thanks.
 

iworkonline

Registered User.
Local time
Today, 09:43
Joined
May 25, 2010
Messages
44
Set a reference to Microsoft HTML Object Library
 

Users who are viewing this thread

Top Bottom