Figure 1     Common Objects

Object Properties/Methods Collections Events
window document, history, location, event, navigate, alert, navigator, scrollBy, open, style frames onLoad, onFocus, onUnload
document body, title, cookie, bgColor, parentWindow, write, open, insertAdjacentHTML, style all, forms, images, links, applets, filters onClick, onKeyDown, onHelp, onMouseOver
navigator appName, appVersion mimeTypes (none)
a href, host, hash, innerText, style all onClick
form submit, action, method, style all, children onSubmit
text accessKey, tabIndex, blur, focus, style all onFocus, onBlur
checkbox accessKey, tabIndex, blur, focus, style all onFocus, onBlur
div click, blur, focus, style all onFocus, onBlur
img src, style all onLoad, onClick


Figure 3   Common Objects and Classes

Object MSHTML Class Name
window HTMLWindow2
document HTMLDocument
navigator HTMLNavigator
a HTMLLinkElement
form HTMLFormElement
text HTMLTextElement
checkbox HTMLCheckboxElement
div HTMLDivElement
img HTMLImg


Figure 4   Inspecting Elements in an HTML Document


 Option Explicit
 
 Private Sub cmdBack_Click()
     On Error Resume Next
     WebBrowser1.GoBack
 End Sub
 
 Private Sub cmdForward_Click()
     On Error Resume Next
     WebBrowser1.GoForward
 End Sub
 
 Private Sub cmdGo_Click()
     WebBrowser1.Navigate txtAddress
 End Sub
 
 Private Sub Form_Load()
     WebBrowser1.Navigate "http://www.microsoft.com"
 End Sub
 
 Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
     
     On Error Resume Next
     If (pDisp Is WebBrowser1.Object) Then
         'DocumentComplete event is fired for each frame on a page
         'this condition means that the main document is fully loaded
         'and you can use brwWebBrowser.Document property
         
         txtAddress = WebBrowser1.LocationURL
         Me.Caption = WebBrowser1.LocationName
         txtText = ""
         tvTreeView.Nodes.Clear
         RecurseFrames WebBrowser1.Document, Nothing
     End If
 End Sub
 
 
 Private Sub RecurseFrames(ByVal iDoc As HTMLDocument, ByVal iNode As Node)
     Dim I As Integer
     Dim Range As IHTMLTxtRange
     Dim Title As String
     Dim TextInfo As String
     Dim tvNode As Node
     
     On Error Resume Next
     
     Title = iDoc.Title
     If Title = "" Then
         Title = iDoc.parentWindow.Name
     End If
     
     If iNode Is Nothing Then
         'if this is the first time, add a root node
         Set tvNode = tvTreeView.Nodes.Add(, , , Title)
     Else
         Set tvNode = tvTreeView.Nodes.Add(iNode.Index, tvwChild, , Title)
     End If
     
     TextInfo = "Frame: " & Title & vbCrLf & "{" + vbCrLf
     
     'check to see if the document has a BODY
     If iDoc.body.tagName = "BODY" Then
         'fill the tree with following collections
         FillTree iDoc, "OBJECT", tvNode, "ActiveX Controls"
         FillTree iDoc, "A", tvNode, "Anchors"
         FillTree iDoc, "IMG", tvNode, "Images"
         FillTree iDoc, "", tvNode, "All"
         
         'use the text range object to get text out of BODY
         Set Range = iDoc.body.createTextRange
         TextInfo = TextInfo & Range.Text & vbCrLf
         Set Range = Nothing
     End If
     
     txtText.Text = txtText.Text & TextInfo & "}" & vbCrLf
     
     'recurse all the frames
     For I = 0 To iDoc.frames.length - 1
         RecurseFrames iDoc.frames(I).Document, tvNode
     Next
 End Sub
 
 Private Sub FillTree(iDoc As HTMLDocument, iMatchTag As String, iNode As Node,
                      iCategory As String)
     Dim Element As Object
     Dim Info As String
     Dim tvNode As Node
     Dim tvCatNode As Node
     
     On Error Resume Next
     
     Set tvCatNode = Nothing
     For Each Element In iDoc.All
         'if the tag is the desired tag or all tags are desired
         If iMatchTag = "" Or Element.tagName = iMatchTag Then
             
             Info = Element.tagName & " "
             
             'display information based on the tagName of the element
             If Element.tagName = "IMG" Then
                 Info = Info & Element.href
             ElseIf Element.tagName = "A" Then
                 Info = Info & Element.innerText & " (" & Element.href & ")"
             ElseIf Element.tagName = "INPUT" Then
                 Info = Info & Element.Type
             ElseIf Element.tagName = "META" Then
                 Info = Info & Element.Name
             Else
                 Info = Info & Element.id
             End If
             
             If tvCatNode Is Nothing Then
                'add the category node if its not there
                 Set tvCatNode = tvTreeView.Nodes.Add(iNode.Index, tvwChild, ,
                                                      iCategory)
             End If
             Set tvNode = tvTreeView.Nodes.Add(tvCatNode.Index, tvwChild, , Info)
         End If
     Next
End Sub

Figure 5   Dynamically Creating Elements


 Option Explicit
 
 Private WithEvents mDoc As HTMLDocument
 Attribute mDoc.VB_VarHelpID = -1
 Private WithEvents mButton As HTMLInputButtonElement
 Attribute mButton.VB_VarHelpID = -1
 
 
 Private Sub UserControl_Show()
     Dim Elem As IHTMLElement
     Dim HTML As String
  
     On Error Resume Next
     
     'set the mDoc var to our document so we can receive events
     Set mDoc = UserControl.Parent.Script.document
     
     mDoc.Title = "Dynamically Created Page"
   
     'start building our HTML in a string
     HTML = "<B>IE4 Document Object Model Sample</B><BR>"
     HTML = HTML & "This page is created dynamically from within a VB control.  All
     the "HTML = HTML & "events are processed in the VB control as well.  Do a View
     Source to see for yourself.<BR>"
           
     HTML = HTML & "<A ID=LINK1 HREF=""""> Display VB MsgBox</A><BR>"
     HTML = HTML & "<A ID=LINK2 HREF=""""> Change caption of this link</A><BR>"
     HTML = HTML & "<A ID=LINK3 HREF=""""> Navigate somewhere</A><P>"
     
     HTML = HTML & "<FORM ID=FORM1 METHOD=POST>"
     HTML = HTML & "<INPUT NAME=FIELD1 TYPE=TEXT><BR>"
     HTML = HTML & "<INPUT NAME=FIELD2 TYPE=TEXT><BR>"
     HTML = HTML & "<INPUT ID=GO TYPE=BUTTON VALUE=""Go"">"
     HTML = HTML & "</FORM>"
     
     'Create the page with HTML
     'find this control so we can insert text after the control
     Set Elem = mDoc.All("TEST")
     Elem.insertAdjacentHTML "AfterEnd", HTML
     
     Set mButton = mDoc.All("GO")
             
 End Sub
 
 
 Private Function mDoc_onclick() As Boolean
     Dim Ev As IHTMLEventObj
     Dim S As String
     
     'this click event is called when ever a click event from any element
     'in the document is bubbled up.  The event information is in the Event object
     'associated with the document's window object
     Set Ev = mDoc.parentWindow.event
         
     Select Case Ev.srcElement.id
         Case "LINK1"
             MsgBox "Simple MsgBox from VB Code"
             Ev.cancelBubble = True
         
         Case "LINK2"
             S = InputBox("Enter new caption for the link")
             If S <> "" Then
                 mDoc.All("LINK2").innerText = S
             End If
             Ev.cancelBubble = True
         
         Case "LINK3"
             S = InputBox("Where do you want to go today?")
             If S <> "" Then
                 mDoc.parentWindow.location.href = S
             End If
             
         Case Else
             Ev.cancelBubble = False
     End Select
    
 End Function
 
 
 Private Function mDoc_onhelp() As Boolean
     MsgBox "Display your help from here"
 End Function
 
 
 Private Function mButton_onclick() As Boolean
     MsgBox "You entered: " & vbCrLf & _
            "Field1=" & mDoc.Forms("FORM1").children("FIELD1").Value & vbCrLf & _
            "Field2=" & mDoc.Forms("FORM1").children("FIELD2").Value
 End Function
 
 
 Private Sub UserControl_Terminate()
     Set mDoc = Nothing
     Set mButton = Nothing
 End Sub
 
 
 Sub OutputDebug(iStr As String)
     Dim H As Integer
 
     H = FreeFile
     Open App.Path + "\" + App.EXEName + "_Debug.TXT" For Append As #H
     Print #H, iStr
     Close #H
 End Sub