문제

When i am calling below macro from vbscript why i am getting type mismatch error

Macro in parameter.xlsm

Sub Proc(sParam1 As String, iParam2 As Integer)
MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub

VB Script code

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
도움이 되었습니까?

해결책 2

Try declaring your variables explicitly in the vbscript... I also used a long in the workbook code out of habit.

Code inside parameter.xlsm:

Option Explicit
Sub Proc(sParam1 As String, iParam2 As Long)
    MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub

VBScript code:

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

다른 팁

In your procedure you are concatenating strings but the second parameter is of type integer. You need to cast to a string first using the CStr() function

Sub Proc(sParam1 As String, iParam2 As Integer)
MsgBox sParam1 & " is " & CStr(iParam2) & " Years Old"
End Sub
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top