jc123456
jc123456

Reputation: 33

VBA navigating IE javascript link

First post. No idea how to do this. Never retrieved data from a website before. Can navigate through VBA to a page with options to open reports, but at this point everything goes from html to Javascript to open reports. How would I go about opening the report? No problems in navigating to the page or pulling down the data from the table

<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>

How would i navigate to the control report for example?

Any help appreciated, thankyou

Upvotes: 3

Views: 20442

Answers (1)

Soulfire
Soulfire

Reputation: 4296

You can use the IE.Navigate method (assuming here that IE is your InternetExplorer object). You would do something like IE.Navigate "javascript:handleHyperlink('control.do')"

It's important to note that the page's HTML document will be reloaded (or rather, it was the one time I needed to use this), therefore any variables you have referencing HTML Elements on the page will have to be re-assigned after you use the Navigate method.

A more complete example:

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

This is just how I've found to do it based on making some code work for my own needs. There may be an even better way.

Addendum

You can execute JavaScript functions by using two methods (that I know of). The first is the execScript and the other is FireEvent. Remember that IE is your InternetExplorer object.

execScript

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

Additionally, since the js code is tied to a button, you can also use the FireEvent as such:

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

This method assumes that there is only one button with the name "showAllLinesButton". If that is not the case, you'd have to adjust the index of 0 in the .Item(0) to be the index of the correct button that needs clicked.

I have not tested either method, and I am not completely sure of the advantages/drawbacks of one approach over the other.

Upvotes: 7

Related Questions