javascript - 从 VBA 调用 JavaScript

标签 javascript vba excel

首先,我不是程序员,我是一名金融专业的学生,​​所以我制作了一个带有宏的 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 系列-EQ 时间段从 - 1/1/2016 时间段至 - 1/12/2016

最佳答案

我将 IE 更改为对象并将 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

这种方式适合我。

Reference image

关于javascript - 从 VBA 调用 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46038953/

相关文章:

excel - 在 VBA 中引用事件表

excel - 如何从 VBA/Excel 中的 MSXML.ResponseText 读取 CRLF 分隔行

excel - 通过 VBA 代码将多张纸导出为 .pdf

excel - 使用公式格式化 Excel 单元格中的文本子集

excel - 根据 Excel 中两个不相邻单元格的值创建范围

javascript - crafty.js 中 Sprite 上显示的背景

javascript - Grunt Uglify 最后一行的奇怪字符导致没有任何变化

javascript - 如何在 fabricjs canvas 对象上触发 mousedown 事件?

javascript - ExtJs 4 : Component with Ext. ux.upload 插件以 MVC 风格重写后停止工作

vba - 显示和隐藏事件工作表中的形状