[英]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: 两个可能的问题:
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.