|
-
Aug 19th, 2008, 05:38 PM
#1
Thread Starter
New Member
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
-
Aug 20th, 2008, 06:24 AM
#2
Re: VBA Web Controls
Welcome to the forums. 
Are you getting errors or does it just not do anything?
-
Aug 20th, 2008, 07:02 AM
#3
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")
-
Aug 20th, 2008, 01:56 PM
#4
Thread Starter
New Member
Re: VBA Web Controls
 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?
-
Aug 20th, 2008, 02:09 PM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|