How to call a javascript file (.js) via Excel VBA?

How to call a javascript file (.js) via Excel VBA?


Since I am against the same problem, I will try to present my case to you guys.

I am trying to automate retrieving data from valeo directory using excel vba macro.

I have a list of Valeo car related products (huge list like over 3000 thousand items). And I would like to import directly information from a directory, which seems to work under javascript.

The data I need is a list of all vehicles attached to the link.

Here is the url: http://outcat-cs.tecdoc.net/ows/en/7FA2A0C501BC34CA4BECB04095663CF1.ows_cs2.srv?view=VIndexFramesetJsp

I would like to access the "Direct article search" tab to copy the link directly from the excel tab cell and then simulate a click on the link to display the "linked vehicles section" and then copy them to a new excel sheet.

I already had time to do this with the html of a clean programmed web page (oscaro.com) using the following code:

Set maPageHtml = IE.document
Set Helem = maPageHtml.getElementsByTagName("input")

For i = 0 To Helem.Length - 1 
    If Helem(i).getAttribute("name") = "toFind" Then Helem(i).Value = "819971" '819971 is the valeo reference searched
    If Helem(i).getAttribute("name") = "submit" Then Set Monbouton = Helem(i)
Next

Monbouton.Click 'this does the click on my button Monbouton

      

But this technique cannot be used with the valeo website as I cannot (or at least not yet know how to do it) to select / click a button when the page is rendered in javascript, since it does not have name, value, or identifier for the button.

It also seems that the URL in the address field is the same as clicking the "Direct Article Search" button and after clicking ....

Hope I'm clear enough in spite of my english ...

Hello

+1


source to share


3 answers


All the previously suggested approaches seem like hack to me.



For a more robust solution, embed Javascript in a COM component via Windows Script Components and invoke Javascript-based COM like any other COM component.

+3


source


I don't think there is a direct way to run JavaScript code in VBA.

What you can try to do is paste JavaScript code into an HTML form that you can open in a (hidden) browser. Then fill the form controls through the browser control's DOM and submit the form. The view runs the JavaScript function you want to call.

Example (not tested):

VBA:



oIE.Document.frmMain.param1.Value = 5
oIE.Document.frmMain.param2.Value = "6"
oIE.Document.frmMain.submit.click ' this line will call the JavaScript function

      

HTML:

<div id="submit" > <a href="javascript:doAction();">Do Action</a></div>

<script>
function doAction()
{
    // do whatever the code should do
}
</script>

      

0


source


Do you mean through a Windows scripting host? You can copy (use shell command) in wscript.exe with a .js file name. But this javascript object model will not be like the one you get in the browser. It would be helpful if you could tell us what javascript was supposed to do.

0


source







All Articles