使用VBA将数组传递给网页中的脚本函数

您好我试图导入数据从网页http://www.investing.com/indices/us-spx-500-futures-historical-data我想要做的是一个macros导航到这个网页,并select一个日历的date范围不同。

我不明白如何在日历中设置date。 我可以打开它,但我不能“复制”鼠标点击selectdate到目前为止,我开发的代码是:

Sub Problem() Dim IE As Object Set IE = CreateObject("InternetExplorer.application") IE.Visible = True IE.navigate "http://www.investing.com/indices/us-30-historical-data" ' PauseTime = 4 ' Set duration to wait in seconds. Start = Timer ' Set start time. Do ' While Timer < Start + PauseTime DoEvents ' allow other processes to work (the browser to have time to load the webpage) Loop Until IE.ReadyState = READYSTATE_COMPLETE Or Timer > Start + PauseTime IE.Document.getElementByID("datePickerIconWrap").Click ' End Sub 

我查看了这个页面的源代码,并且发现了两个有趣的函数: DatePickerGetDateDatePickerSetDate,我试图运行这两个脚本

 IE.Document.parentWindow.execScript "widgetHolCalendar.DatePickerGetDate", "javascript" IE.Document.parentWindow.execScript "widgetHolCalendar.DatePickerSetDate", "javascript" 

代码没有给出错误消息,但没有任何改变,所以我不确定代码是否真的在执行。 如果我已经正确的理解了代码,为了设置一个新的date,我必须用2个参数调用DatePickerSetDate

 DatePickerSetDate(date, shifTo) 

其中date是一个2个元素的数组,shifTO是一个布尔值。 我不知道如何传递数组到这个脚本使用VBA。

此外,当我调用函数DatePickerGetDate我想获得结果并保存在一个VBA数组

谁能帮忙?

这工作对我来说(从页面完全加载时拿起…)

 Dim f As String 'set the new dates (you just need to plug the required strings in here... f = "$('#widgetHolCalendar').DatePickerSetDate(['11/05/2013', '11/12/2013'], true);" IE.document.parentWindow.execScript f, "jscript" 'trigger the page function which refreshes the table f = "historical_submit();" IE.document.parentWindow.execScript f, "jscript" 

这是一个关于我的build议上面的自定义的代码示例:

 <!DOCTYPE html> <html> <head> <title>Playing with Data in a Date range</title> <!-- load the jQuery CSS first. The order is important here. Css first, then jQuery, then ui and at last plugins remove the ".min" if you are on development environment (this let gets you human readable JS Lib Code) --> <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script> <!-- load the datables plugin --> <script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js" type="text/javascript"></script> <script type="text/javascript"> var oTable; function applyChangesAction(sender) { // TODO: Retrieve the data from the server and render it into the table // they are many ways to do it with datatables. Here is a good // discussion about it: http://datatables.net/forums/discussion/comment/26734 // after doing that you should be able to refresh the whole table by simply calling this below: //oTable.fnReloadAjax(); } $(document).ready(function() { // >>> BEGIN DATEPICKER LOGIC <<< var iOneDayInMs = 60 * 1000 * 60 * 24; $("#dtPickTo").datepicker( { "setDate": new Date(), "maxDate": new Date() } ); $("#dtPickFrom").datepicker( { "maxDate": new Date($("#dtPickTo").datepicker("getDate").getTime() - iOneDayInMs) } ); // >>> END DATEPICKER LOGIC <<< // >> BEGIN DATATABLES LOGIC << oTable = $("#dataTabPrices").dataTable( { "bSortClasses": false, "bProcessing": true, "bServerSide": true, "sAjaxSource": "http://path/to/your/json/return_script.php", "fnServerData": function(sSource, aoData, fnCallback) { // put your filter criteria data in this object below to get the requested results: var oaData = { "dtPickFrom": $("#dtPickFrom"), "dtPickTo": $("#dtPickTo") }; $.ajax( { "dataType": "json", "type": "POST", "url": sSource, "data": JSON.stringify(aoData), // your data filtering criteria for the server side script to return only requested data "success": fnCallback // closure callback if all data is received and ready to be rendered into the table (refresh logic for button click) } ); } } ); // >> END DATATABLES LOGIC << } ); </script> </head> <body> <p> <label for="dtPickFrom">From</label><input type="text" id="dtPickFrom" name="dtPickFrom" /><br /> <label for="dtPickTo">Until</label><input type="text" id="dtPickTo" name="dtPickTo" /> <br /> <input type="button" id="btnApplyChanges" name="btnApplyChanges" value="Apply" onClick="applyChangesAction(this);" /> </p> <hr /> <table id="dataTabPrices"> <thead> <tr> <th>Date</th> <th>Last</th> <th>Open</th> <th>High</th> <th>Low</th> <th>Change &#37;</th> </tr> </thead> <tbody> </tbody> </table> </body> </html> 

这还没完成! 这只是一个基本的例子,你应该怎么做。 阅读我的意见,看看到底是什么东西,让它充分发挥作用。 我的代码已经过testing,可以工作到目前为止…

我希望这有帮助!