首先,我不是程序员,我是一名金融专业的学生,所以我制作了一个带有宏的 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
这种方式适合我。
关于javascript - 从 VBA 调用 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46038953/