最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling a javascript file (.js) via Excel VBA? - Stack Overflow

programmeradmin1浏览0评论

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


So as i am opposed to the same kind of problem i'll try to submit you guys my case.

I am trying to automate datas extraction from valeo's catalogue using excel vba macro.

I have a list of références attached to valeo's automotive products (huge list, as more than 3000 thousands items). And i would like to import directly informations from the catalogue wich seems to run under javascript.

The datas i need is the list of every vehicules attached to a reference.

Here is the url: .ows_cs2.srv?view=VIndexFramesetJsp

I'd like to access to the "Direct Article Search" tab, in order to copy a reference directly from an excel tab's cell and then simulate a clic on the reference in order to display the "linked vehicules section" and then to copy them in a new excel sheet.

I already succeede in doing this with html pure programmed webpage (oscaro) 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 can't be used with valeo website since I am not able (or at least I don't know yet how to do it) to select/click a button when the page is made on javascript, since it doesn't have a name, value or id for the button.

Also it seems that the url in the address field is the same before clicking on the "Direct Article Search" button and after having clicked....

Hope i am clear enought in spite of my english...

Greetings

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


So as i am opposed to the same kind of problem i'll try to submit you guys my case.

I am trying to automate datas extraction from valeo's catalogue using excel vba macro.

I have a list of références attached to valeo's automotive products (huge list, as more than 3000 thousands items). And i would like to import directly informations from the catalogue wich seems to run under javascript.

The datas i need is the list of every vehicules attached to a reference.

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

I'd like to access to the "Direct Article Search" tab, in order to copy a reference directly from an excel tab's cell and then simulate a clic on the reference in order to display the "linked vehicules section" and then to copy them in a new excel sheet.

I already succeede in doing this with html pure programmed webpage (oscaro.) 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 can't be used with valeo website since I am not able (or at least I don't know yet how to do it) to select/click a button when the page is made on javascript, since it doesn't have a name, value or id for the button.

Also it seems that the url in the address field is the same before clicking on the "Direct Article Search" button and after having clicked....

Hope i am clear enought in spite of my english...

Greetings

Share Improve this question edited Jun 12, 2018 at 19:27 braX 11.8k5 gold badges22 silver badges37 bronze badges asked Dec 4, 2008 at 16:00 JohnJohn 1
  • 1 Some explanation of what you want to do behind the scenes would be really helpful. – Tomalak Commented Dec 4, 2008 at 16:08
Add a ment  | 

3 Answers 3

Reset to default 4

All the previously suggested approaches sound hacky to me.

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

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>

You mean through windows scripting host? You can shell (use the shell mand) 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.

发布评论

评论列表(0)

  1. 暂无评论