How can I pass variable parameters into a Javascript Function using Excel VBA

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m not sure if I have the correct terms above, but what I m trying to do is call a Javascript function passing parameters into it from an Excel File, so the function will run and list the items in the next textbox, allowing me to choose the correct entry in that textbox, which then chooses the final box.

Here is the code that I have, that works, if I don t put variable into it: Call .Document.parentWindow.execScript("FillVendorNames( MyText )", "javascript") If I put in any variable in instead of MyText , I get a run time error: Call .Document.parentWindow.execScript("FillVendorNames(cCode)", "javascript") Call .Document.parentWindow.execScript("FillVendorNames(.document.all.ComCode)", "javascript") The variables are declared earlier in the code and I can check to see if the values are correct using the immediates window and they are all correct, but I still get the runtime error.

What I m trying to do is use the existing function which autofills a dropdown list, based on the option chosen in the original dropdown list. If I choose MyText in the first dropdown, then FillVendorNames gives the list of vendors in the next dropdown, allowing me to choose it. I can then enter the next choice in the next function and it picks a third option, but this is all based on the first function creating the second drop down list. The first list is autoloaded on the page, but the second isn t, so I can t choose an option from it. Can anyone help, please? Thanks.

Answers

Two possible issues:

    • VBA doesn t replace variable names with their values within strings

Example:

Dim x as String
x = "Hello world!"

Msgbox x
// shows "Hello world!"

Msgbox "x"
// shows "x"
    • Call is never required (because you can just omit parentheses instead) and can cause problems

Example:

Call Msgbox("x")
// is almost exactly the same as
Msgbox "x"

Just use:

With foo
    // do stuff
    .Document.parentWindow.execScript "FillVendorNames(" & cCode & ")", "javascript"
    // do more stuff
End With

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1032858/how-can-i-pass-variable-parameters-into-a-javascript-function-using-excel-vba

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils