All pastes #816268 Raw Edit

vbgetform

public text v1 · immutable
·published 2007-12-14 18:57 UTC
rendered paste body

Private Function getDoc(ByVal formName As String) As MSHTML.HTMLDocument
    
    Dim xdoc1 As MSHTML.HTMLDocument
    Dim xdoc2 As MSHTML.HTMLDocument
    Dim xdoc3 As MSHTML.HTMLDocument
    Dim xdoc4 As MSHTML.HTMLDocument
    Dim xdoc5 As MSHTML.HTMLDocument
    Dim xdoc6 As MSHTML.HTMLDocument
    Dim xitm1 As MSHTML.IHTMLElement
    Dim xitm2 As MSHTML.IHTMLElement
    Dim xitm3 As MSHTML.IHTMLElement
    Dim xitm4 As MSHTML.IHTMLElement
    Dim xitm5 As MSHTML.IHTMLElement
    Dim xitm6 As MSHTML.IHTMLElement
    Dim xcol1 As MSHTML.IHTMLElementCollection
    Dim xcol2 As MSHTML.IHTMLElementCollection
    Dim xcol3 As MSHTML.IHTMLElementCollection
    Dim xcol4 As MSHTML.IHTMLElementCollection
    Dim xcol5 As MSHTML.IHTMLElementCollection
    Dim xcol6 As MSHTML.IHTMLElementCollection
    
    'find the first document that has a form named 'form'
    WebBrowser1.SetFocus
    
    'lets get this goin...
    Set xdoc1 = WebBrowser1.Document
    Set xcol1 = xdoc1.getElementsByTagName("*")
    
    For Each xitm1 In xcol1
        If (UCase(xitm1.getAttribute("tagName")) = "FORM" _
            And UCase(xitm1.getAttribute("name")) = UCase(formName)) Then
            Set getDoc = xdoc1
            Exit Function
        ElseIf ((xitm1.getAttribute("tagName") = "IFRAME") Or (xitm1.getAttribute("tagName") = "FRAME")) Then
            MsgBox ("FOUND FRAME" & xdoc1.frames.Item(xitm1.getAttribute("id")).Document)
    '        Set xdoc2 = xdoc1.frames.Item(xitm1.getAttribute("id")).Document
    '        Set xcol2 = xdoc2.getElementsByTagName("*")
    '
    '        For Each xitm2 In xcol2
    '            If (UCase(xitm2.getAttribute("tagName")) = "FORM" _
    '                And UCase(xitm2.getAttribute("name")) = UCase(formName)) Then
    '                Set getDoc = xdoc2
    '                Exit Function
    '            ElseIf ((xitm2.getAttribute("tagName") = "IFRAME") Or (xitm2.getAttribute("tagName") = "FRAME")) Then
    '                Set xdoc3 = xdoc2.frames.Item(xitm2.getAttribute("id")).Document
    '                Set xcol3 = xdoc3.getElementsByTagName("*")
    '
    '                For Each xitm3 In xcol3
    '                    If (UCase(xitm3.getAttribute("tagName")) = "FORM" _
    '                        And UCase(xitm3.getAttribute("name")) = UCase(formName)) Then
    '                        Set getDoc = xdoc3
    '                        Exit Function
    '                    ElseIf ((xitm3.getAttribute("tagName") = "IFRAME") Or (xitm3.getAttribute("tagName") = "FRAME")) Then
    '                             Set xdoc4 = xdoc3.frames.Item(xitm3.getAttribute("id")).Document
    '                             Set xcol4 = xdoc4.getElementsByTagName("*")
    '
    '                            For Each xitm4 In xcol4
    '                                If (UCase(xitm4.getAttribute("tagName")) = "FORM" _
    '                                    And UCase(xitm4.getAttribute("name")) = UCase(formName)) Then
    '                                    Set getDoc = xdoc4
    '                                    Exit Function
    '                                ElseIf ((xitm4.getAttribute("tagName") = "IFRAME") Or (xitm4.getAttribute("tagName") = "FRAME")) Then
    '                                    Set xdoc5 = xdoc4.frames.Item(xitm4.getAttribute("id")).Document
    '                                    Set xcol5 = xdoc5.getElementsByTagName("*")
    '
    '                                    For Each xitm5 In xcol5
    '                                        If (UCase(xitm5.getAttribute("tagName")) = "FORM" _
    '                                            And UCase(xitm5.getAttribute("name")) = UCase(formName)) Then
    '                                            Set getDoc = xdoc5
    '                                            Exit Function
    '                                        End If
    '                                    Next
    '                                End If
    '                            Next
    '                     End If
    '                 Next
    '            End If
    '        Next
        End If
    Next

    MsgBox ("Can't find form named '" & formName & "'. Going with base doc.")

    Set getDoc = xdoc1 'just in case we dont find one inside an iframe
    
End Function