如何使用 Excel VBA 将可变参数传递给 Javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1032858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:30:52  来源:igfitidea点击:

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

javascriptvbafunctionparametersvariables

提问by DOS

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.

我不确定上面的术语是否正确,但我想要做的是调用一个 Javascript 函数,将参数从 Excel 文件传递​​给它,因此该函数将运行并在下一个文本框中列出项目,允许我在该文本框中选择正确的条目,然后选择最后一个框。

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.

这是我拥有的代码,如果我不将变量放入其中,则它有效: Call .Document.parentWindow.execScript("FillVendorNames('MyText')", "javascript")如果我放入任何变量而不是“MyText”,Call .Document.parentWindow.execScript("FillVendorNames(cCode)", "javascript") Call .Document.parentWindow.execScript("FillVendorNames(.document.all.ComCode)", "javascript")则会出现运行时错误: 变量在代码中更早地声明并且我可以使用立即数窗口检查这些值是否正确并且它们都正确,但我仍然收到运行时错误。

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.

我想要做的是使用现有的功能,根据原始下拉列表中选择的选项自动填充下拉列表。如果我在第一个下拉列表中选择 MyText,则 FillVendorNames 在下一个下拉列表中提供供应商列表,允许我选择它。然后我可以在下一个函数中输入下一个选项,它会选择第三个选项,但这都是基于创建第二个下拉列表的第一个函数。第一个列表会自动加载到页面上,但第二个不是,因此我无法从中选择一个选项。请问有人可以帮忙吗?谢谢。

采纳答案by barrowc

Two possible issues:

两个可能的问题:

  • VBA doesn't replace variable names with their values within strings
  • VBA 不会用字符串中的值替换变量名

Example:

例子:

Dim x as String
x = "Hello world!"

Msgbox x
// shows "Hello world!"

Msgbox "x"
// shows "x"
  • Callis never required (because you can just omit parentheses instead) and can cause problems
  • Call永远不需要(因为您可以省略括号)并且可能会导致问题

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