从VBA调用JavaScript

我不是一名程序员,我是一名财务学生,所以我用macros创build一个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 

注 – input参数为符号 – SBIN系列 – 均衡时间从 – 1/1/2016时间段到 – 1/12/2016

我改变了IE对象,并修复oEleCol.ClickoEle.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 

这样它对我有用。

参考图像