Solved

HTML object library/ WebBrowser object

Posted on 2001-06-14
4
269 Views
Last Modified: 2012-06-21
I'm writing a function to scan through an HTML document object, and find an element based on the arguments being passed in.

Public Function GetHTMLElement(htmlDocument as HTMLDocument,  sElementName as String, Optional sElementType as String)

Dim oElement as HTMLhtmlElement
Dim i as Integer

If sElementType <> "" then
For Each oElement in htmlDocument.getElementsByTagName
???
Next oElement
End If

For i = 1 To htmlDocument.Frames.length
     ? = GetHTMLelement(htmlDocument.Frames(i).Document, "string", "string2")
next i

End function


This is all I have so far.  I'm interested in returning a collection of elements that match arg2 and arg3.  How do I populate this function with each element that matches the criteria?  Thanks for any commets/suggestions.
0
Comment
Question by:vt45
  • 2
4 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 6190889
try this:

On Error Resume Next

Dim oCollection As New Collection

For Each oElement In htmlDocument
   If Ucase$(oElement.name) = UCase$(sElementName) Then
      If UCase$(oElement.type) = UCase$(sElementType) Then
         oCollection.Add oElement
      End If
   End If
Next
0
 

Author Comment

by:vt45
ID: 6192583
Azra-

That works for one document, but if a particular webpage contains x frames, that means x document objects.  I want this function to keep calling itself until it has looped through all subframes and at the same time populating the collection the function is defined as.

Public Function GetHTMLElement(htmlDocument as HTMLDocument,  sElementName as String, Optional sElementType
as String) as Collection

Dim oElement as HTMLhtmlElement
Dim i as Integer

If sElementType <> "" then
For Each oElement in htmlDocument.getElementsByTagName
???
Next oElement
End If

For i = 1 To htmlDocument.Frames.length
    ? Set GetHTMLelement = GetHTMLelement(htmlDocument.Frames(i).Document, "string", "string2") 'i get a type mismatch here
next i

End function
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6193027
try:
GetHTMLElement(htmlDocument.Frames.Item(i).Document, "string", "string2")
0
 
LVL 9

Accepted Solution

by:
kprestage earned 100 total points
ID: 6193636
Try this...


Public Static Function GetHTMLElements(htmlDocument As Object, Optional sElementTagName As String, Optional sElementAttribute As String, Optional sElementName As String, Optional NewCall As Boolean = True) As Collection
Dim oElement As HTMLHtmlElement
Dim i As Integer
Dim o As Object
Static x As Collection

If NewCall Then Set x = New Collection
Set GetHTMLElements = New Collection

If sElementTagName <> "" Then
    If sElementAttribute <> "" Then
        For Each oElement In htmlDocument.getElementsByTagName(sElementTagName)
            If UCase(oElement.Attributes(sElementAttribute)) = UCase(sElementName) Then
                x.Add oElement
            End If
        Next oElement
    Else
        For Each oElement In htmlDocument.getElementsByTagName(sElementTagName)
            x.Add oElement
        Next oElement
    End If
Else
    For Each oElement In htmlDocument.All
        x.Add oElement
    Next oElement
End If

If htmlDocument.frames.length > 0 Then
    For i = 0 To htmlDocument.frames.length - 1
        Set o = htmlDocument.frames(i).document
        GetHTMLElements o, sElementTagName, sElementAttribute, sElementName, False
    Next i
End If
Set GetHTMLElements = x

End Function

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now