当我从vbscript调用下面的宏时,为什么我会遇到类型不匹配错误
parameter.xlsm中的宏
Sub Proc(sParam1 As String, iParam2 As Integer)
MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub
VB脚本代码
Dim objExcel,objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\ExcelFiles\parameter.xlsm")
sParam1 = Inputbox("Enter the first parameter")
iParam2 = Inputbox("Enter the second parameter")
iParam3= CInt(iParam2)
objExcel.Application.Visible = True
objExcel.Application.Run "parameter.xlsm!Proc",sParam1,iParam3
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Finished."
WScript.Quit
答案 0 :(得分:0)
在您的过程中,您是串联字符串,但第二个参数是整数类型。您需要首先使用CStr()函数
转换为字符串Sub Proc(sParam1 As String, iParam2 As Integer)
MsgBox sParam1 & " is " & CStr(iParam2) & " Years Old"
End Sub
答案 1 :(得分:0)
尝试在vbscript中明确声明变量...我还习惯在工作簿代码中使用long
。
parameter.xlsm中的代码:
Option Explicit
Sub Proc(sParam1 As String, iParam2 As Long)
MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub
VBScript代码:
Dim objExcel As Object
Dim objWorkbook As Workbook
Dim sParam1 As String
Dim iParam2 As Long, iParam3 As Long
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\ExcelFiles\parameter.xlsm")
sParam1 = InputBox("Enter the first parameter")
iParam2 = InputBox("Enter the second parameter")
iParam3 = CInt(iParam2)
objExcel.Application.Visible = True
objExcel.Application.Run "parameter.xlsm!Proc", sParam1, iParam3
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Finished."
WScript.Quit