【问题标题】:NSE India Quote History Download AutomationNSE India 报价历史 下载自动化
【发布时间】:2017-08-31 08:43:36
【问题描述】:

首先我不是程序员,我只是想通过在 Excel 中输入一些输入数据来从https://www.nseindia.com/products/content/equities/equities/eq_security.htm 下载报价历史。我以某种方式设法将数据放入 VBA。谁能帮助我如何点击“以 CSV 格式下载文件”并将其保存到我的本地磁盘。

这是我的 VBA 代码:

Private Sub CommandButton1_Click()
  Dim IE As Object
  With IE
  Set IE = CreateObject("InternetExplorer.Application")

'create new instance of IE. use reference to return current open IE if
'you want to use open IE window. Easiest way I know of is via title bar.
  IE.Navigate "https://www.nseindia.com/products/content/equities/equities/eq_security.htm"
'go to web page listed inside quotes
  IE.Visible = True
  While IE.busy
    DoEvents  'wait until IE is done loading page.
  Wend
  IE.document.ALL("symbol").Value = ThisWorkbook.Sheets("sheet1").Range("b1")
  IE.document.ALL("series").Value = ThisWorkbook.Sheets("sheet1").Range("b2")

  IE.document.getElementById("rdDateToDate").Click

  IE.document.ALL("fromDate").Value = ThisWorkbook.Sheets("sheet1").Range("b4")

  IE.document.ALL("toDate").Value = ThisWorkbook.Sheets("sheet1").Range("c4")
  IE.document.getElementById("submitMe").Click

  End With

End Sub

【问题讨论】:

  • 您能提供SymbolSeries 的值吗?理想的 From DateTo Date 也是如此,但它们并不重要
  • Symbol=SBIN, Series=EQ, From date=01-01-2012 & To Date=01-12-2012
  • 请提供下载文件后自动关闭IE的代码。谢谢。

标签: vba excel internet-explorer


【解决方案1】:

在 UDF 下面会做你想要的:

Private Sub CommandButton1_Click()
    Dim IE As New InternetExplorer
    Dim oW As Worksheet: Set oW = ThisWorkbook.Worksheets("Sheet4")
    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("I2")
        .document.all("series").Value = oW.Range("I3")
        .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("I4")
        .document.all("toDate").Value = oW.Range("I5")

        ' 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.innerText = "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

        ' Download file
        DownloadFile .hwnd

        ' Close IE
        .Quit

    End With

End Sub

注意:
1. 你可以把这行注释掉,因为我用它来最大化浏览器窗口:ShowWindow .hwnd, SW_SHOWMAXIMIZED
2、DownloadFile是一个函数调用。你可以在这里找到函数:How to download a file from internet explorer using VBA
3. 将工作表名称更改为您的工作表名称
4. 将单元格引用更改为您的引用是什么

【讨论】:

  • 我怀疑是 IE 对象引发了错误。那是因为我使用的是Microsoft Internet Controls,您在其中创建了一个 IE 对象。通过单击Tools 菜单引用上述控件,然后单击References。这将打开一个窗口。向下滚动直到找到Microsoft Internet Controls,选择它然后按OK 按钮。这应该可以解决问题
  • 我已经更新了代码,这样您就不会再遇到这些错误了。不要忘记从我提供的链接中获取 DownloadFile 函数
  • 我不知道在下载文件链接中输入什么代码。你能帮我包括哪些代码。
  • 我尝试在没有 DownloadFile 代码的情况下启动代码,但它没有弹出文件下载选项,即代码没有点击“以 csv 格式下载文件”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多