我试图使用getElementByID函数简单地将内容或innertext粘贴到excel中。内容实际上是iframe链接,我试图将其提取并粘贴到单元格中。显示的照片是html源代码。
Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim result As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set htmldoc = ie.document
Set Results = HTML.getElementById("bm_ann_detail_iframe")
Sheets("Sheet1").Range("a1").Value = Results.innerText
End Sub
答案
您应该在代码中使用一致的变量命名。如果您将Option Explicit放在代码顶部,这将有所帮助。
您想要访问src的iframe属性以获取显示的URL。
如果您打算使用新网址,那么您实际上需要“#”之前的部分。这意味着改为:
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Split(ie.document.getElementById("bm_ann_detail_iframe").src, "#")(0)
码:
Option Explicit
Public Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
ThisWorkbook.Worksheets("Sheet1") = ie.document.getElementById("bm_ann_detail_iframe").src
ie.Quit
End Sub