如何使用Excel VBA将可变参数传递到Javascript函数中 [英] How can I pass variable parameters into a Javascript Function using Excel VBA

查看:316
本文介绍了如何使用Excel VBA将可变参数传递到Javascript函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我有的代码,如果我不' t把变量放入它:
调用.Document.parentWindow.execScript(FillVendorNames('MyText'),javascript)
如果我放入任何变量而不是'MyText',我得到一个运行时错误:
调用.Document.parentWindow.execScript(FillVendorNames(cCode),javascript)
调用.Document.parentWindow.execScript(FillVendorNames(.document.all.ComCode),javascript)

变量在代码的前面声明,我可以查看如果使用立即窗口的值是正确的,并且它们都是正确的,但我仍然会遇到运行时错误。



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

解决方案




  • VBA不会用字符串中的值替换变量名称



示例:

  Dim x as String 
x =Hello world!

Msgbox x
//显示Hello world!

Msgboxx
//显示x




  • 呼叫从不需要(因为您可以忽略括号)并可能导致问题


$ b

示例:

 呼叫Msgbox(x)
//几乎和
一样Msgboxx

只需使用:

 用foo 
//做东西
.Document.parentWindow.execScriptFillVendorNames(& cCode& ),javascript
//做更多的事情
End With


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.

解决方案

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

这篇关于如何使用Excel VBA将可变参数传递到Javascript函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆