3

I have a function in an Excel file that returns a custom class, when i try to call this function from VBScript i get an "Unknown runtime error"

Sample VBA Code:

Function myVBAFunc() As Variant
   Set myClass1 = New Class1
   myClass1.Name = "Test"
   Set myVBAFunc= myClass1
End Function

Calling VBScript code:

Set Excel = CreateObject("Excel.Application") 
Set Workbook = Excel.Workbooks.open("C:\myFile.xls")
Set myRes = Excel.Run("myVBAFunc")
Excel.Quit

I am getting an error when i try to set myRes, I assume this is caused by VBScript not recognizing the object's type. Is there any way to work around this?

Thanks, Achraf

1 Answer 1

3

You need to change the instancing property of the class module (in its property window) from Private to PublicNotCreatable.

(You can also strictly type the function)

Function myVBAFunc() As Class1
   Set myVBAFunc= New Class1
   myVBAFunc.Name = "Test"
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's much better now, I can access the object and use it. However as soon as the Excel object is closed i can no longer use the object! Is there anyway to copy the object locally to VBS so i can use it without keeping Excel open? thx
I doubt very much you can do that at all, if you don't need excels functionality at all give the class a method of returning whatever data you need from it and use it in vbs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.