Results 1 to 5 of 5

Thread: VBA Web Controls

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    VBA Web Controls

    I'm trying to use the code below to retrieve data from a webpage. I have my program navigate to the webpage then send the DOM to the sub I've shown below. It seems to me like this should work, but it doesn't. I've tried just about every variation possible, and I can't find anything to make it work. Any ideas that you might have would be greatly appreciated. Thanks!

    Code:
    Sub CAMSAnalyzer(HTMLDoc As HTMLDocument)
        'Use to sift through the results returned by CAMS
        'and identify the relevant account information
        Dim AllResults As HTMLGenericElement
        Dim TRElements As HTMLGenericElement
        Dim TDElements As HTMLGenericElement
        Dim ClassName As String
        Dim Att As String
        Dim Val As String
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        k = 0
        j = 0
        i = 0
                    
        With Range("A3").Select
        Set AllResults = HTMLDoc.getElementsByTagName("*") 'this is where my problem is!
        For Each TRElements In AllResults.getElementsByTagName("tr")
            For Each TDElements In TRElements.getElementsByTagName("td")
                If TDElements.getAttribute("class") <> Null Then
                ClassName = TDElements.getAttribute("class")
                    If ClassName = "dm-attribute" Then
                        Att = "Some Attribute"
                        ActiveCell.Offset(i + 3, 0).Value = Att
                        i = i + 1
                    End If
                    If ClassName = "dm-value" Then
                        Val = "Some Value'"
                        ActiveCell.Offset(i + 3, 1).Value = Val
                    End If
                End If
                j = j + 1
            Next TDElements
            k = k + 1
        Next TRElements
        End With
        Range("C2").Value = "j = " & j
        Range("C3").Value = "k = " & k
    End Sub

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA Web Controls

    Welcome to the forums.

    Are you getting errors or does it just not do anything?

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: VBA Web Controls

    I know not much about this but I think with

    Dim AllResults As HTMLGenericElement
    ...
    Set AllResults = HTMLDoc.getElementsByTagName("*")


    the RHS will return a collection HtmlElementCollection
    while the LHS is a HTMLGenericElement that is not a collection.
    Perhaps that causes an error.

    Why you need AllResults?
    Can we have just

    For Each TRElements In HTMLDoc.getElementsByTagName("tr")
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: VBA Web Controls

    Quote Originally Posted by anhn
    I know not much about this but I think with

    Dim AllResults As HTMLGenericElement
    ...
    Set AllResults = HTMLDoc.getElementsByTagName("*")


    the RHS will return a collection HtmlElementCollection
    while the LHS is a HTMLGenericElement that is not a collection.
    Perhaps that causes an error.

    Why you need AllResults?
    Can we have just

    For Each TRElements In HTMLDoc.getElementsByTagName("tr")
    Thanks for your suggestions. I've already tried that though, and it doesn't seem to work. Also, the second suggestion you gave doesn't seem to work either. That's why I tried adding the AllResults portion of the code.

    Any other ideas?

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: VBA Web Controls

    Actually, it looks like I spoke too soon. The latter suggestion did work (I must have had a typo the last time I tried it). However, it still seems to have a glitch: it's not recognizing any TRElement or TDElement that has an attribute "class" other than Null.

    There are several <tr> and <td> tags inside the html document I'm trying to analyze. What I'm particularly interested in are the tags:

    <tr class="dm-attribue"> and <td class="dm-value">

    My program, however, doesn't seem to be recognizing these tags. I've tried changing the <> Null to <> "" and it doesn't seem to work either. Any ideas would be appreciated. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width