Reputation: 130
I'm trying to return the pageYOffset value from an Internet Explorer window. In my Locals window in the VBA Editor I can clearly see the value but when I try to retrieve it with VBA, I get the Run-time error '438': Object doesn't support this property or method.
Here is a simplified version of my code:
Sub getProperty()
Dim IEWindow As New InternetExplorer
Dim scrollValue as Long
Set IEWindow = New InternetExplorer
IEWindow.Visible = True
IEWindow.navigate "www.somewebsite.com", TargetFrameName:="_parent"
Sleep 1000
'Scrolls to very bottom of page (approximate)
IEWindow.document.parentWindow.Scroll 0, 5000
'Retrieves the exact scroll value
scrollValue = IEWindow.document.parentWindow.pageYOffset
End Sub
I've scoured the web looking for answers but nothing VBA related seems to show up which makes me think I'm looking in the wrong places.
Upvotes: 0
Views: 663
Reputation: 1
I came up with an idea to use javascript and unused attribute document.body.title
IE.document.parentWindow.execScript "document.body.title=document.parentWindow.pageYOffset;"
scrollValue = 1 * IE.document.body.title
Upvotes: 0
Reputation: 4977
I have to say, I find the objects and properties in the InternetExplorer
object really difficult to manage. Perhaps it's overkill but I find it easier to develop with early binding and define each object explicitly - it's the only way I can find to expose the properties.
If I don't do it this way then I get the type of problem that you have encountered. I don't know why the locals window would show the methods and properties but the code throws an error when I try to access them. Perhaps someone more knowledgeable than me could explain it.
In the meantime, perhaps your code would work if you did define each object explicitly, like so:
'References:
' - Microsoft Internet Controls
' - Microsoft HTLM Object Library
Dim IEWindow As New InternetExplorer
Dim doc As HTMLDocument
Dim wnd As HTMLWindow2
Dim scrollValue As Long
Set IEWindow = New InternetExplorer
IEWindow.Visible = True
IEWindow.navigate URL:="www.somewebsite.com", TargetFrameName:="_parent"
Do Until IE.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Set doc = IE.Document
Set wnd = doc.parentWindow
scrollValue = wnd.pageYOffset
Upvotes: 1