Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2012, 07:15 AM
tinfanide tinfanide is offline IE9- "document.getElementsByClassName" problem Windows 7 64bit IE9- "document.getElementsByClassName" problem Office 2010 32bit
Expert
IE9- "document.getElementsByClassName" problem
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default IE9- "document.getElementsByClassName" problem

Code:
Function w(cName As String, q As String)

On Error Resume Next
Dim IE As New InternetExplorer
IE.Visible = False
IE.navigate "http://hk.dictionary.yahoo.com/dictionary?p=" & q
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim html As HTMLDocument
Set html = IE.document

Dim y As Integer
y = -1

Do Until y > html.getElementsByTagName("div").Length
    y = y + 1
    If html.getElementsByTagName("div")(y).ClassName = cName Then _
      
       w = html.getElementsByTagName("div")(y).innerText
    End If
Loop

IE.Quit
On Error GoTo 0


End Function
Well, I've originally written a set of VBA codes with the use of "document.getElementsByClassName" but it does not work in my users' computers on which they've installed IE9- (mine is IE9)



I thought ClassName only works in IE9 so I tweaked the code above a bit and hoped to have a workaround. It works. It is far from perfect, though.
(Use of "document.getElementsByClassName" can save me work on looping the tags to find where the classname is.

I wonder if there's a direct approach for it. (I can do it in JS but very good in VB)
Reply With Quote
  #2  
Old 01-11-2012, 10:06 AM
macropod's Avatar
macropod macropod is offline IE9- "document.getElementsByClassName" problem Windows 7 64bit IE9- "document.getElementsByClassName" problem Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi tinfanide,

The document.getElementsByClassName parameter is available in IE9 / HTML5. So, if it's not working on your users' PCs, it would seem that either their IE9 installation is faulty or they don't actually have IE9 installed.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-12-2012, 06:17 AM
tinfanide tinfanide is offline IE9- "document.getElementsByClassName" problem Windows 7 64bit IE9- "document.getElementsByClassName" problem Office 2010 32bit
Expert
IE9- "document.getElementsByClassName" problem
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Hi tinfanide,

The document.getElementsByClassName parameter is available in IE9 / HTML5. So, if it's not working on your users' PCs, it would seem that either their IE9 installation is faulty or they don't actually have IE9 installed.
Code:
Option Explicit

Function GetElementsByClassName1(cName As String, q As String)


Dim IE As New InternetExplorer
IE.Visible = True
IE.navigate "http://hk.dictionary.yahoo.com/dictionary?p=" & q
Dim html As HTMLDocument
Set html = IE.Document

Do Until IE.readyState = 4

Loop
    
    Dim Result
    Dim tag
    Dim tags As Object
    Set tags = html.getElementsByTagName("*")

    Result = Array()


    For Each tag In tags
        If tag.className = cName Then
            ReDim Preserve Result(UBound(Result) + 1)
            Set Result(UBound(Result)) = tag
        End If
    Next tag

    GetElementsByClassName1 = Result(0).getElementsByTagName("div")(0).innerText
    
IE.Quit
End Function
GetElementsByClassName.xlsm
Could ya please debug the code for me?
It seems that I cannot pass the arguments directly "strings" (with reference to the error help in Excel)
It's beyond what I know about VBA.
Reply With Quote
  #4  
Old 01-12-2012, 06:50 AM
tinfanide tinfanide is offline IE9- "document.getElementsByClassName" problem Windows 7 64bit IE9- "document.getElementsByClassName" problem Office 2010 32bit
Expert
IE9- "document.getElementsByClassName" problem
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Well, I've made another try and the result was more weird.

GetElementsByClassName.xlsm

If I debug it in VBA Editor, it returns the results in each cell.
But when I run it as a macro, it does not return any value.
Reply With Quote
  #5  
Old 01-12-2012, 07:18 AM
tinfanide tinfanide is offline IE9- "document.getElementsByClassName" problem Windows 7 64bit IE9- "document.getElementsByClassName" problem Office 2010 32bit
Expert
IE9- "document.getElementsByClassName" problem
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Just fix it.
Code:
Sub WebDictQuery()

Dim x As Integer
Dim Query As String
For x = 1 To 3
   Cells(x, 3).Value = GetElementsByClassName("pronunciation", Cells(x, 1).Value)
Next x

End Sub

Function GetElementsByClassName(className, q)
''''''''''''''''''''''''''''''''''''''''
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
''''''''''''''''''''''''''''''''''''''''
IE.Visible = False
IE.navigate "http://hk.dictionary.yahoo.com/dictionary?p=" & q

Do Until IE.readyState = 4

Loop
    
    Dim Result
    Dim tag
    Dim tags As Object
'''''''''''''''''''''''''''''''''''''''''''''''''''
    Set tags = IE.document.getElementsByTagName("*")
'''''''''''''''''''''''''''''''''''''''''''''''''''
    Result = Array()

    For Each tag In tags
        If tag.className = className Then
            ReDim Preserve Result(UBound(Result) + 1)
            Set Result(UBound(Result)) = tag
        End If
    Next tag

    GetElementsByClassName = Result(0).getElementsByTagName("div")(0).innerText
    
IE.Quit
''''''''''''''''''''''''''''''''''''
Set IE = Nothing
''''''''''''''''''''''''''''''''''''
End Function
The key is late binding. I might have got it wrong, though. Earlier I used to use early binding for the Internet Explorer Object (don't know if I may get it right or wrong...) It reports an error in the line

Code:
Set html = IE.Document
But when it is set with late binding, nothing wrong happens.
Reply With Quote
  #6  
Old 01-13-2012, 12:43 AM
macropod's Avatar
macropod macropod is offline IE9- "document.getElementsByClassName" problem Windows 7 64bit IE9- "document.getElementsByClassName" problem Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi tinfanide,

I'm not familiar with IE automation. However, when I test your funnction with:
Code:
Sub Test()
GetElementsByClassName1 "pronunciation", "dog"
End Sub
it fails every time at 'Set html = IE.Document' with the error message "Runtime Error '2147467259 (80004005)': / Automation Error / Unspecified Error". You can fix that by moving the
'Set html = IE.Document' after the 'Loop' line. Speaking of which your 'Do Until ... Loop' should have a 'DoEvents' statement, like:
Code:
Do Until IE.readyState = READYSTATE_COMPLETE
  DoEvents
Loop
After moving the 'Set html = IE.Document' line, the code runs to completion OK, but there is still no sound - which is what I gather you're intending to get.

Your code would also be more efficient with:
Code:
Result = Array(0)
For Each tag In tags
  If tag.className = cName Then
    Set Result(0) = tag
    Exit For
  End If
Next tag
GetElementsByClassName1 = Result(0).getElementsByTagName("div")(0).innerText
and even moreso if you dispense with the array:
Code:
For Each tag In tags
  If tag.className = cName Then
    GetElementsByClassName1 = tag.getElementsByTagName("div")(0).innerText
    Exit For
  End If
Next tag
None of which resolves the underlying problem, but might help with the project overall.

PS: I'm moving the thread to the Excel forum, as you've posted in the Word vba forum and your attachment shows this is really for Excel.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 01-14-2012, 08:05 AM
tinfanide tinfanide is offline IE9- "document.getElementsByClassName" problem Windows 7 64bit IE9- "document.getElementsByClassName" problem Office 2010 32bit
Expert
IE9- "document.getElementsByClassName" problem
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Yes, thanks for your input.
I've forgotten the use of Exit For and without this it'll make the application work unnecessarily after it finishes its job.
Yes, the use of array here is not necessary.
The most amazing part is that "set" has to be placed after the loop event.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to edit the "Format" and the "show level" of an EXISTING table of content? Jamal NUMAN Word 2 08-14-2011 10:46 AM
IE9- "document.getElementsByClassName" problem Word 2003 document suddenly "locked for editing" by me lisa801 Word 3 07-08-2011 10:11 PM
IE9- "document.getElementsByClassName" problem "Table of content" based on "Normal Style" behavior!!!! Jamal NUMAN Word 4 07-08-2011 04:12 AM
IE9- "document.getElementsByClassName" problem How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM
"Microsoft Excel Application" missing in the "Component Services" on win08 sword.fish Excel 0 02-26-2010 02:09 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft