如何使用Excel VBA将变量参数传递到Javascript函数中

时间:2022-07-25 18:00:01

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.

以下是我所拥有的代码,如果我没有将变量放入其中,则可以使用:调用.Document.parentWindow.execScript(“FillVendorNames('MyText')”,“javascript”)如果我输入任何变量'MyText',我得到一个运行时错误:调用.Document.parentWindow.execScript(“FillVendorNames(cCode)”,“javascript”)调用.Document.parentWindow.execScript(“FillVendorNames(.document.all.ComCode)” ,“javascript”)变量在代码中先前声明,我可以使用immediates窗口检查值是否正确并且它们都是正确的,但我仍然得到运行时错误。

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会在下一个下拉列表中显示供应商列表,允许我选择它。然后我可以在下一个函数中输入下一个选项并选择第三个选项,但这都是基于创建第二个下拉列表的第一个函数。第一个列表在页面上自动加载,但第二个列表没有,因此我无法从中选择一个选项。有人可以帮忙吗?谢谢。

1 个解决方案

#1


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"
  • 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

#1


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"
  • 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