2014-01-06 49 views
1

它在循環顯示「詳細信息藍色」類名時一直失敗,我想要獲取附加圖像等數據。Excel Vba,無法循環顯示文本列

Do While (.Busy Or .readystate <> 4) 
DoEvents 
Loop 

    For Each ele In .document.all 

If ele.classname = "details_title" Then 
    RowCount = RowCount + 1 
    sht.Range("A" & RowCount) = ele.innertext 


      ElseIf ele.classname = "details blue" Then 
      i = 1 
      For x = 0 To ele.Length 'error at this line 
      i = i + 1 
      sht.Cells(RowCount, i).Value = ele.Item(x).innertext 'error at this line 
      Next x 


End If 
Next ele 

=

+0

具有u嘗試ele.innertext –

+0

該頁面有多個classname = details藍色,我需要收集所有的innertext,如果我使用「ele.innertext」它只會得到最後一個 – user3164150

+0

你試過了嗎?For x = 0 To(ele.Length- 1))' –

回答

0

您正在閱讀在時間的元素一成ele所以(一)不會有,因爲它是一個元素不是一個集合是一個length財產*和(b)你不會同時獲得所有「詳細藍色」元素

我懷疑你可能想要使用getElementsByClassName方法。你可以用這個代替整個For Each eleNext ele業務:

Dim detailsTitles As IHTMLElementCollection 
Dim detailsBlues As IHTMLElementCollection 

Set detailsTitles = document.getElementsByClassName("details_title") 

' There should only be one of these 
If (detailsTitles.length = 1) Then 
    RowCount = RowCount + 1 
    sht.Range("A" & RowCount) = detailsTitles.item(0).innerText 
Else 
    ' do something - e.g. report an error to the user 
End If 

Set detailsBlues = document.getElementsByClassName("details blue") 

i = 1 
For Each ele in detailsBlues 
    i = i + 1 
    sht.Cells(RowCount, i).Value = ele.innerText 
Next ele 

相反,如果你確實有多個「details_title」項目這反過來有多個相關的「細節藍色」項目,那麼這將需要不同的方法(可能尋找在「details_title」項目的parentElement


*某些元素可能有一個屬性叫做length但是這不是一回事