HTMLStyle Object: How to get The Name, Value Pairs
Hello Everyone! I hadn't been able to come around for a while but now I am back with an interesting question (At least for me :lol: )
Anyway, Let's think you are checking each tag in a HTML Document Object and you get its HTMLStyle Object (oTag.Style) and now you want to know which of the styles are set (Example: color:blue; border: 1px solid black)
I haven't found a way to do this without having to check the whole properties contained... Is there a way to iterate through all of them without having to type the whole list and checking every single one of them?
I thought about getting the outerHTML and parsing the style but I would prefer to see if any of you can enlighten me with a clever way.
Putting it short: I want to check which tag objects have in-line styles and which are their names and values. I am using an Internet Explorer application to do the HTML Parsing.
Thank you very much for your time
Re: HTMLStyle Object: How to know its contents
How about posting url of a sample web page so we will all be referencing the same material.
1 Attachment(s)
Re: HTMLStyle Object: How to know its contents
You are right... I didn't have a specific page in mind thinking this could be used for any HTML document but let me add something here.
We could also try this page:
http://www.w3.org/TR/html401/struct/links.html
Thank you for your help! :wave:
Re: HTMLStyle Object: How to know its contents
Here is what I had in mind doing if there was no better way.
Maybe it will help people to see what I am trying to do...
VB Code:
Dim bHasStyle As Boolean
Public Sub parseFile(sFileName As String)
Dim iE As Object
Dim HTMLDoc As Object
Dim oTag As Object
Set iE = New InternetExplorer.Application
iE.Navigate(sFileName)
Do while iE.readystate <> 4
DoEvents
Loop
Set HTMLDoc = iE.document
For Each oTag in HTMLDoc.All
processTag oTag
Next
Set oTag = Nothing
Set HTMLDoc = Nothing
Set iE = Nothing
End Sub
Private sub processTag(oTag As Object)
Dim iSSt As Integer
Dim sStyle As String
Dim Style As Object
Set Style = New Scripting.Dictionary
'Get the Index of the style="
iSSt = InStr(1, oTag.outerHTML, " style=""") + 8
If iSSt > 8 Then
'Get the string isnde the style attribute:
sStyle = Trim(Mid(oTag.outerHTML, iSSt, InStr(iSSt, oTag.OuterHTML, """") - iSSt))
'Begins Style gathering:
Dim i As Integer, iUBound As Integer, szStyles As String(), szNameValue As String()
szStyles = Split(sStyle, ";") 'Get all the styles contained in the style string
iUBound = UBound(szStyles)
'Loop the Array of Styles and split them in Name and Value separated by :
For i = 0 To iUBound
szNameValue = Split(szStyles(i), ":")
If UBound(szNameValue) > 0 Then 'Ensure it is not an empty array
If Style.Exists(szNameValue(0)) Then
Style(szNameValue(0)) = szNameValue(1)
Else
Style.Add szNameValue(0), szNameValue(1)
End If
End If
Next
bHasStyle = iUBound >= 0
'Ends Style gathering
'Process the style data...
End If
Set Style = Nothing
End Sub
Thank you, guys.