【发布时间】:2017-08-18 17:00:08
【问题描述】:
我一直在尝试编写单击已放置在 HTML 表中的图像的 excel VBA 代码。我试图点击的图像只有源代码。
我尝试了 4 种不同的方法来点击图片,但都没有奏效。谁能帮我写VBA代码?
1.
Dim HTMLDoc As Object
Set HTMLDoc = IE.document
Dim ElementCol As Object
Set ElementCol = HTMLDoc.getElementsByTagName("td")
For Each link In ElementCol
If link.innerHTML = "excel.jpg" Then
link.Click
Exit For
End If
Next link
2.
Dim HTMLDoc As Object
Set HTMLDoc = IE.document
Dim i As Object
For Each i In HTMLDoc.images
If i.src = "../images/excel.jpg" Then
i.Click
Exit for
End If
Next i
3.
Dim HTMLDoc As Object
Set HTMLDoc = IE.document
Dim img As Object
For Each img In HTMLDoc.all
If img.innerHTML = "excel.jpg" Then
img.Click
Exit For
End If
Next img
4.
Dim HTMLDoc As Object
Set HTMLDoc = site.document
Dim tdCollection As Object
Set tdCollection = HTMLDoc.all
For Each cell In tdCollection
If cell.ID = "singleXLS" Then
cell.Click
Exit Sub
End If
Next
这是 HTML 代码。
<tbody><tr>
<td id="singleXLS" onclick="javascript:gCognosViewer.getRV().viewReport('spreadsheetML');" return="" true;"="" style="cursor:hand;" valign="middle" align="center">
<img src="../images/excel.jpg" border="0"></td>
<td> </td>
按照建议,这是我正在使用的代码。
Sub click_img()
Dim URL As String
Dim HTMLDoc As Object
Set site = CreateObject("internetexplorer.application")
URL = Range("B2").Value 'There is an URL in cell B2
site.navigate URL
site.Visible = True
While site.busy
Wend
Do Until site.readyState = 4
DoEvents
Loop
Set HTMLDoc = site.Document
HTMLDoc.getElementById("singleXLS").getElementsByTagName("img")(0).Click 'Error is here
End Sub
【问题讨论】:
-
HTMLDoc.getElementbyId("singleXLS").getElementsbyTagName("img")(0).click
-
感谢蒂姆的快速回复。我试过这个。但我收到运行时错误 424:需要对象。你能帮忙吗?
-
鉴于您提供的 HTML,我发布的代码应该可以工作。您可以发布您当前的代码会有所帮助。
-
我已经粘贴了我正在使用的代码(最后一节)。请检查并建议是否需要更正。
标签: javascript jquery html vba excel