2014-11-04 81 views
0

在VBA中,如果我使用getElementByID(「id_name」)並且id不存在,則該函數不返回空值。這讓我不知道DOM是否還沒有呈現元素,或者元素是否真的不存在。看起來規範要求返回一個NULL,並且NULL不等於Nothing。所以我的問題是這個DOM函數是否返回NULL,Nothing或者它是否依賴於我失蹤的東西?由於爲什麼getElementByID返回Nothing而不是NULL

snipit

If Not IsNull(p_IE.Document.getElementById(MAIN_SR_CONTAINER)) Then 
' If I do not receive NULL I want to assume that I can grab the element. 
' Still, I verify that the element is not Nothing 

' problem is that NULL <> Nothing so if the element does not exist my code loops for eternity 
' I do look at the readystate of the p_IE object and wait till it = 4 
' But the elements may be being created by embedded javascript on the fly 

    Set elMainSRContainer = p_IE.Document.getElementById(MAIN_SR_CONTAINER) 
    Do While elMainSRContainer Is Nothing 
     Set elMainSRContainer = p_IE.Document.getElementById(MAIN_SR_CONTAINER) 
    Loop 

    : 
    : 
Else 
    ' bail 
End If 

回答

0

MSDN documentation for getElementById說,該方法的返回值的類型IHTMLElement。這將是VBA中的Object類型。文檔繼續說,方法

返回具有指定ID的第一個對象,如果不匹配則返回null。

我的猜測是因爲,在VBA,Objects不能抱Null所以Null被解釋爲Nothing

我會嘗試改變

If Not IsNull(p_IE.Document.getElementById(MAIN_SR_CONTAINER)) Then 

If Not (p_IE.Document.getElementById(MAIN_SR_CONTAINER) Is Nothing Then