[英]Obtain Internet Explorer Window properties (pageYOffset) in excel-VBA
I'm trying to return the pageYOffset value from an Internet Explorer window.我正在尝试从 Internet Explorer 窗口返回 pageYOffset 值。 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.
在 VBA 编辑器的本地窗口中,我可以清楚地看到该值,但是当我尝试使用 VBA 检索它时,出现运行时错误“438”:对象不支持此属性或方法。
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.我在网上搜寻答案,但似乎没有任何与 VBA 相关的内容出现,这让我觉得我找错了地方。
I have to say, I find the objects and properties in the InternetExplorer
object really difficult to manage.不得不说,我发现
InternetExplorer
对象中的对象和属性真的很难管理。 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我想出了一个使用 javascript 和未使用的属性 document.body.title 的想法
IE.document.parentWindow.execScript "document.body.title=document.parentWindow.pageYOffset;"
scrollValue = 1 * IE.document.body.title
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.