【问题标题】:Call JavaScript from VBA从 VBA 调用 JavaScript
【发布时间】:2017-09-04 22:44:02
【问题描述】:

首先我不是程序员,我是金融专业的学生,​​所以我制作了一个带有宏的 Excel 文件,https://www.nseindia.com/products/content/equities/equities/eq_security.htm 的所有字段都会自动填充。但是我无法单击如何单击“以 csv 格式下载文件”。点击链接的 VBA 代码是什么?

Private Sub CommandButton1_Click()
Dim IE As New InternetExplorer
Dim oW As Worksheet: Set oW = ThisWorkbook.Worksheets("Sheet1")
Dim oEleCol As MSHTML.IHTMLElementCollection
Dim oEle As MSHTML.IHTMLElement


With IE
'Set IE = CreateObject("InternetExplorer.Application")

    ' Set IE
    .Visible = True
    'ShowWindow .hwnd, SW_SHOWMAXIMIZED

    ' Navigate to URL
    .Navigate "https://www.nseindia.com/products/content/equities/equities/eq_security.htm"

    ' Wait for page to be ready
    While .Busy
      DoEvents  'wait until IE is done loading page.
    Wend

    ' Set criteria
    .document.all("symbol").Value = oW.Range("B1")
    .document.all("series").Value = oW.Range("B2")
    .document.getElementById("rdDateToDate").Click

    ' Wait for page to be ready
    While .Busy
      DoEvents  'wait until IE is done loading page.
    Wend

    ' Set remaining criteria
    .document.all("fromDate").Value = oW.Range("B3")
    .document.all("toDate").Value = oW.Range("D3")

    ' Submit criteria
    .document.getElementById("submitMe").Click

    ' Wait for page to be ready
    While .Busy
      DoEvents  'wait until IE is done loading page.
    Wend

    ' Find the link to download file
    Set oEleCol = .document.getElementsByTagName("A")
    For Each oEle In oEleCol
        If oEle.innerHTML = "Download file in csv format" Then
            oEleCol.Click
            Exit For
        End If
    Next

    ' Wait for page to be ready
    While .Busy
      DoEvents  'wait until IE is done loading page.
    Wend

End With

End Sub

注意 - 输入参数将是 符号 - SBIN 系列 - 情商 时间段 - 2016 年 1 月 1 日 时间段至 - 2016 年 1 月 12 日

【问题讨论】:

  • 请添加您的代码以便改进。
  • 应该oEleCol.ClickoEle.Click 吗?
  • oEle.Click,但这部分代码不起作用。
  • “在 csv 文件中下载文件”的 HTML 代码如下所示 下载csv格式文件

标签: javascript vba excel


【解决方案1】:

我将 IE 更改为 object 并将 oEleCol.Click 固定为 oEle.Click 并且它起作用了。但是它要求确认保存,因此您必须启用自动确认或寻找如何实现Send Keys

Private Sub CommandButton1_Click()
Dim IE As Object
Dim oW As Worksheet: Set oW = ThisWorkbook.Worksheets("Plan1")
'Dim oEleCol As MSHTML.IHTMLElementCollection
'Dim oEle As MSHTML.IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")

With IE
'Set IE = CreateObject("InternetExplorer.Application")

    ' Set IE
    .Visible = True
    'ShowWindow .hwnd, SW_SHOWMAXIMIZED

    ' Navigate to URL
    .Navigate "https://www.nseindia.com/products/content/equities/equities/eq_security.htm"

    ' Wait for page to be ready
    While .Busy
      DoEvents  'wait until IE is done loading page.
    Wend

    ' Set criteria
    .document.all("symbol").Value = oW.Range("B1")
    .document.all("series").Value = oW.Range("B2")
    .document.getElementById("rdDateToDate").Click

    ' Wait for page to be ready
    While .Busy
      DoEvents  'wait until IE is done loading page.
    Wend

    ' Set remaining criteria
    .document.all("fromDate").Value = oW.Range("B3")
    .document.all("toDate").Value = oW.Range("D3")

    ' Submit criteria
    .document.getElementById("submitMe").Click

    ' Wait for page to be ready
    While .Busy
      DoEvents  'wait until IE is done loading page.
    Wend

    ' Find the link to download file
    Set oEleCol = .document.getElementsByTagName("A")
    For Each oEle In oEleCol
        If oEle.innerhtml = "Download file in csv format" Then

            oEle.Click
            Exit For
        End If
    Next

    ' Wait for page to be ready
    While .Busy
      DoEvents  'wait until IE is done loading page.
    Wend

End With

End Sub

这种方式对我有用。

【讨论】:

  • 对我来说效果很好。尝试在'Find the link to download file' 之前添加Application.Wait(Now + TimeValue("00:00:01"))
  • 在代码末尾的 End Sub 之前添加 `Application.Wait Now + TimeValue("00:00:01") Application.SendKeys "%+s", True`。看看是否有效
  • 问题在于,据我所知,该按钮不可能被识别为变量。是否可以执行所有宏,然后手动单击“是”?还有其他工具,例如 Mouse recorder 可以帮助完成这项任务
  • %+s 将是 Alt+S,这是下载窗口上保存按钮的热键。这与通过 VBA 可以获得的一样接近。也许您可以尝试将 Internet Explorer 配置为自动接受任何下载?
  • 实际上,我必须为我的研究下载数以千计的数据,这就是为什么我一直在努力实现一些自动化以减少时间,尽管如此多的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 2019-04-18
  • 2018-05-23
  • 1970-01-01
相关资源
最近更新 更多