Solved

HTML object library/ WebBrowser object

Posted on 2001-06-14
4
274 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 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