[英]Obtain Internet Explorer Window properties (pageYOffset) in excel-VBA
我正在尝试从 Internet Explorer 窗口返回 pageYOffset 值。 在 VBA 编辑器的本地窗口中,我可以清楚地看到该值,但是当我尝试使用 VBA 检索它时,出现运行时错误“438”:对象不支持此属性或方法。
这是我的代码的简化版本:
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
我在网上搜寻答案,但似乎没有任何与 VBA 相关的内容出现,这让我觉得我找错了地方。
不得不说,我发现InternetExplorer
对象中的对象和属性真的很难管理。 也许这有点矫枉过正,但我发现使用早期绑定进行开发并明确定义每个对象更容易 - 这是我能找到的公开属性的唯一方法。
如果我不这样做,那么我就会遇到您遇到的问题类型。 我不知道为什么本地窗口会显示方法和属性,但是当我尝试访问它们时代码会抛出错误。 也许比我更了解的人可以解释它。
同时,如果您确实明确定义了每个对象,也许您的代码会起作用,如下所示:
'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
我想出了一个使用 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.