【问题标题】:Excel VBA click JavaScript buttonExcel VBA 单击 JavaScript 按钮
【发布时间】:2015-08-17 21:59:44
【问题描述】:

谁能给我看 VBA 代码来点击这个网页上的“复制到剪贴板”按钮?该按钮有一个元素 id="ToolTables_example_0",但我无法弄清楚如何从 Excel 执行脚本。我尝试重新设计我发现的许多不同的 VBA 代码,但没有运气。似乎它应该如此简单!谢谢!

https://spotwx.com/products/grib_index.php?model=nam_awphys&lat=30.26678&lon=-97.76905&tz=America/Chicago&display=table

【问题讨论】:

    标签: javascript vba excel


    【解决方案1】:

    以下代码要求您使用 VBE 的 Tools ► References 命令将 Microsoft HTML 对象库和 Microsoft Internet 控件添加到您的项目中。

    这里有两种点击该按钮的方法。

    Sub clickIt()
        Dim a As Long, u As String, till As Double
        Dim el As MSHTML.IHTMLElement, ie As New SHDocVw.InternetExplorer
    
        u = "https://spotwx.com/products/grib_index.php?model=nam_awphys&lat=30.26678&lon=-97.76905&tz=America/Chicago&display=table"
    
        ie.Silent = True
        ie.Visible = True
    
        ie.navigate u
        Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
        'wait an extra second or so (this usually isn't necessary but helped with this page)
        till = Timer + 1
        Do While Timer < till: DoEvents: Loop
    
        'Method 1: Loop through all the A elements looking for the right ID
        For a = 0 To ie.document.getElementsByTagName("a").Length - 1
            With ie.document.getElementsByTagName("a")(a)
                If .ID = "ToolTables_example_0" Then
                    Debug.Print "found ToolTables_example_0 in a loop"
                    .Click
                    'the table data should be on the clipboard
                    Exit For
                End If
            End With
        Next a
    
        'Method 2: Locate the right A element using its ID directly
        On Error Resume Next
        Set el = ie.document.getElementById("ToolTables_example_0")
        On Error GoTo 0
    
        If Not el Is Nothing Then
            Debug.Print "found ToolTables_example_0 directly"
            el.Click
            'the table data should be on the clipboard
        End If
    
        ie.Quit
        Set ie = Nothing
    
    End Sub
    

    我无法完全测试这一点,因为我的 ie 没有安装 Adob​​e FlashPlayer(我更愿意保持这种状态)。如果您可以手动打开 Internet Explorer,导航到该页面,单击按钮,然后将复制到剪贴板的内容粘贴回您的 Excel 工作表,那么我看不出它为什么不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-03
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多