【问题标题】:VBA to click image placed in a html tableVBA单击放置在html表格中的图像
【发布时间】: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>&nbsp;</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


【解决方案1】:

而不是这个:

Set HTMLDoc = site.Document
HTMLDoc.getElementById("singleXLS").getElementsByTagName("img")(0).Click 

试试:

Dim s, i

Set HTMLDoc = site.Document
Set s = HTMLDoc.getElementById("singleXLS")
If Not s Is Nothing Then
    Set i = s.getElementsByTagName("img")(0)
    If Not i Is Nothing Then
        i.Click
    Else      
        Debug.Print "Img not found!" 
    End If
Else
    Debug.Print "singleXLX not found!"
End If

更多代码,但它会帮助您确定确切的问题

【讨论】:

  • 谢谢蒂姆....现在我收到运行时错误 13:行中的类型不匹配:设置 s = HTMLDoc.getElementById("singleXLS")。很多天以来,我一直在尝试执行此代码,但由于每次都遇到不同的错误,所以我无法执行。有没有其他方法可以点击图片(例如,fireevent Onlcick)?
  • 如果没有 URL 进行测试,很难知道问题出在哪里。您使用的代码与发布的完全一致吗?
  • 对不起,蒂姆,分享网址无济于事,因为它需要用户 ID 和密码才能登录。分享它将是一种违规行为。是的,我完全按照发布的方式使用了代码。仍然出现错误。不知道我的 Excel VBA 出了什么问题..
  • 我知道你不能分享这个网址 - 只是指出如果没有它,很难提出任何更有用的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
相关资源
最近更新 更多