2016-08-16 45 views
0

我試圖從Internet Explorer窗口返回pageYOffset值。 在我的VBA編輯器中的本地窗口中,我可以清楚地看到該值,但是當我嘗試使用VBA檢索它時,出現運行時錯誤'438':對象不支持此屬性或方法。在excel-VBA中獲取Internet Explorer窗口屬性(pageYOffset)

這裏是我的代碼的簡化版本:

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相關似乎表明了這讓我想起我找錯了地方。

回答

1

我不得不說,我發現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 
+0

就像一個魅力@Ambie。非常感謝!這是一件奇怪的事情,你必須明確定義每個對象。但是,如果它的工作,它的作品! – TheGuyOverThere

相關問題