tags:

views:

3430

answers:

3

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

A: 

Hi,

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

What you could try to do is to embed the JavaScript code in an HTML form which you could open in a (hidden) browser control. Then fill the form controls via the DOM of the browser control and submit the form. The submit triggers the JavaScript function that you want to call.

Sample (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>
0xA3
A: 

You mean through windows scripting host? You can shell (use the shell command) out to wscript.exe with the name of the .js file. But this javascript object model won't be like the one you get in the browser. It would be helpful if you told us what the javascript was supposed to do.

Will Rickards
+1  A: 

All the previously suggested approaches sound hacky to me.

For a more reliable solution, embed the Javascript in a COM component via Windows Script Components, and call the Javascript-based COM component as you would any other COM component.

Cheeso