[英]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.