从vbscript调用vba函数(带有参数)并显示结果 [英] Calling vba function(with parameters) from vbscript and show the result

查看:34
本文介绍了从vbscript调用vba函数(带有参数)并显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下vba功能(在Excel文件的模块中)

I have the following vba function ( in a module in Excel file)

Public Function validate_fncname(strFncname As String) As Boolean
.
.
.
validate_fncname  = True
End Function

我写了以下vbscript来命名它:

and I wrote the following vbscript to call it :

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook =  objExcel.Workbooks.Open("C:\Users\uidu8611\Desktop\CAM0500040F10_SW_Quality_Assurance_Report_Template(new_version).xlsm")

objExcel.Application.Visible = True

Dim str
str ="hello"

Dim validate_fncname 

 validate_fncname =  objExcel.Application.Run("'C:\Users\uidu8611\Desktop\CAM0500040F10_SW_Quality_Assurance_Report_Template(new_version).xlsm'!validate_fncname", str)

Wscript.Echo validate_fncname

但是,当我运行脚本时,它会给我该行的类型不匹配错误:

however when I run the script, it gives me type mismatch error for the line:

objExcel.Application.Run("'C:\Users\uidu8611\Desktop\CAM0500040F10_SW_Quality_Assurance_Report_Template(new_version).xlsm'!validate_fncname", str)

尽管类型正确(字符串)

Despite the fact that the type is correct (String)

如果我将其更改为:

objExcel.Application.Run("'C:\Users\uidu8611\Desktop\CAM0500040F10_SW_Quality_Assurance_Report_Template(new_version).xlsm'!validate_fncname(5)")

虽然5是整数,但这并没有给我带来错误!

It doesn't give me an error, although 5 is integer!

我的错误在哪里?

推荐答案

您已经具有您的 validate_fncname 方法,该方法看起来不错(除非将返回值设置为 True ,可能是在 if then 语句中完成的,以避免将所有内容都设置为 True ).

You already have your validate_fncname method, which looks fine (except setting the return value to True should probably be done in an if then statement to avoid setting everything to True).

调用此函数时,您应该只能执行以下操作:

When calling this function, you should just be able to do the following:

Dim validateResult As Boolean
Dim str as String
str = "hello"
validateResult = validate_fncname(str)
Debug.Print validateResult

这篇关于从vbscript调用vba函数(带有参数)并显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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