VBA导航IE的JavaScript链接

第一篇文章。 不知道如何做到这一点。 之前从未从网站检索数据。 可以通过VBA导航到页面打开报告的选项,但在这一点上一切都从HTML到Javascript打开报告。 我将如何去打开报告? 在导航到页面或从表格下拉数据时没有问题

<div id='delivery_reports' class='menu' onMouseover='aMenu(this);'> <table> <tr> <td nowrap><a href="javascript:handleHyperlink('finish.do')" class="mainSubLink">finish Report</a></td> </tr> <tr> <td nowrap><a href="javascript:handleHyperlink('start.do')" class="mainSubLink">start Report</a></td> </tr> <tr> <td nowrap><a href="javascript:handleHyperlink('control.do')" class="mainSubLink">control report</a></td> </tr> <tr> <td nowrap><a href="javascript:handleHyperlink('data_start.do')" class="mainSubLink">data start Report</a></td> </tr> </table> </div> 

我将如何导航到控制报告例如?

任何帮助表示感谢,谢谢

你可以使用IE.Navigate方法(假设IE是你的InternetExplorer对象)。 你会做一些像IE.Navigate "javascript:handleHyperlink('control.do')"

重要的是要注意,页面的HTML文档将被重新加载(或者说,这是我需要使用它的一次),因此,在页面上引用HTML元素的任何variables将不得不在使用后重新分配导航方法。

一个更完整的例子:

 Sub Test() Dim ie As InternetExplorer Dim doc As HTMLDocument Set ie = New InternetExplorer ie.Navigate "http://www.yoururl.com" 'Wait for page to load Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE DoEvents Loop Set doc = ie.document 'Do whatever manipulation of the HTML document you need ie.Navigate "javascript:handleHyperlink('control.do')" 'Wait for page to load Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE DoEvents Loop 'Reassign document Set doc = ie.document 'Further manipulation End Sub 

这就是我发现这样做的基础上做一些代码为我自己的需要工作。 可能有更好的办法。

附录

你可以使用两种方法来执行JavaScript函数(我知道的)。 第一个是execScript ,另一个是FireEvent 。 请记住, IE是您的InternetExplorer对象。

EXECSCRIPT

 IE.document.all.Item Call IE.document.parentWindow.execScript("handleShowAllLines()", "JavaScript") 

另外,由于js代码绑定到一个button,所以你也可以使用FireEvent

 IE.Document.GetElementsByTagName("showAllLinesButton").Item(0).FireEvent("onclick") 

此方法假定只有一个button名称为“showAllLinesButton”。 如果不是这种情况,则必须将.Item(0)的索引0调整为需要单击的正确button的索引。

我还没有testing过任何一种方法,我也不完全确定一种方法的优点/缺点。