2017-01-09 49 views
0

我有下面的代碼,其中,我試圖打開一系列的URL和在從每個URL(例如:http://apps.mohltc.ca/ltchomes/detail.php?id=2588&lang=en)數據拉。最感興趣的我會是那些標記爲「地方醫療整合網絡」,「被許可方」和「特許牀」。的getElementsByTagName返回[對象HTMLParagraphElement]

既然這樣,我想只是拉與標籤名「P」的所有元素和處理數據清理以後。我的代碼目前拉入「[對象HTML段落元素]」,而不是我希望的數組。有人可以解釋爲什麼這是嗎?

Sub ImportLicenseeData() 

Dim ie As Object 
Dim LH As Object 
Dim r As Integer 

Set ie = CreateObject("InternetExplorer.Application") 

For r = 4 To 10 
    With ie 
     ie.Visible = False 
     ie.Navigate Cells(r, "H").Value 
     Do While (ie.Busy Or ie.ReadyState <> 4): DoEvents: Loop 

    Set Doc = ie.Document 

    Set LH = Doc.getElementsByTagName("p") 

    End With 

    Worksheets("Sheet1").Range("J" & r).Value = LH 

Next r 

End Sub 

任何幫助表示讚賞。

+0

'設置LH = Doc.getElementsByTagName( 「P」)'檢索'DispHTMLElementCollection'對象但不陣列。您應該創建數組循環收集。 – omegastripes

回答

0
Dim LH As IHTMLElementCollection 
Dim htmlEle1 as IHTMLElement 

它需要Microsoft HTML Object Library參考。那麼你可以與LH收集元素(這不是一個陣列)進行交互,如下所示:

Set LH = Doc.getElementsByTagName("p") 

For Each htmlEle1 in LH 
    Debug.Print htmlEle1.innerText 
Next htmlEle1 
+0

當我編輯,以反映上述我得到一個類型不匹配錯誤 設置LH = Doc.getElementsByTagName(「p」) 我將如何去調試呢? – JPJP

+1

@JPJP'昏暗LH作爲IHTMLElementCollection' – omegastripes

+0

@omegastripes這是真的'HTMLObjectElement'會工作爲對象的另一個集合,而不是一個集合本身的祖先。我改變了它。 –

0

感謝大家的幫助。我不太熟悉處理HTML元素,所以我最終採用了不同的方法。無論如何欣賞反饋。

經由http://www.ozgrid.com/forum/showthread.php?t=178150

Sub RetrieveHTML() 

Dim rngSelect As Range 
Dim sURL As String 

Set rngSelect = Range("H8", Range("H8").End(xlDown)) 
Debug.Print rngSelect.Address 

Set ie = CreateObject("InternetExplorer.Application") 

For Each c In rngSelect 
    sURL = c.Value 
    With ie 
     .Visible = False 
     .Navigate sURL 
     Do Until .ReadyState = 4 
      DoEvents 
     Loop 
     Do While .Busy: DoEvents: Loop 
      Range(c.Address).Offset(0, 1).Value = ie.Document.DocumentElement.outerHTML 
     End With 
    Next c 

End Sub