简体   繁体   English

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

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

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. 如果我在第一个下拉列表中选择MyText,则FillVendorNames在下一个下拉列表中提供供应商列表,以便我选择它。 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. 谢谢。

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 永远不需要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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Excel VBA将变量参数传递给Javascript函数(2) - How can I pass variable parameters into a Javascript Function using Excel VBA (2) 我无法使用addEventListener Javascript / Jquery将参数传递给函数 - I can not pass parameters to a function using addEventListener Javascript/Jquery 如何在不使用函数参数的情况下将值传递给函数? - How can I pass a value into a function without using function parameters? 如何使用 javascript fetch 将参数传递给 action 方法? - How can i pass a parameters to action method using javascript fetch? Javascript - 将可变参数传递给 Function - Javascript - Pass variable parameters to Function 我该如何通过Smarty函数传递javascript变量? - How can I pass a javascript variable through a function with Smarty? 如何在javascript函数中单击时传递此变量? - How can I pass this variable on click in javascript function? 如何传递php include调用作为javascript函数的变量? - How can I pass a php include call as a variable for javascript function? 如何在不使用参数的情况下在javascript中传递变量? - How can I pass variable in javascript without using parameter? 如何使用remoteFunction将JavaScript变量传递给Grails控制器 - How can I pass javascript variable to grails controller using remoteFunction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM