简体   繁体   中英

Obtain Internet Explorer Window properties (pageYOffset) in excel-VBA

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM