Using Classes in .xla

O

oli

Hello,

I try to use a class, defined in a .xla, in a Excel-Spreadsheet. The
class is defined with instancing PubicNotCreate and have a public
method for the object creation. The reference is done in the .xls, so I
can see the class with her method in the object and project explorer.

-In the .xla:-

Public Function newKlasse1()
Set newKlasse1 = New Klasse1
End Function

Public Property Get helloWorld() As String
Message = "Hello world from klasse1"
End Property

-In the .xls:-

Private Sub test_klass()
Dim anObj As Klasse1
Set anObj = *newKlasse1()*
Debug.Print "From klasse1 : " & anObject.helloWorld
End Sub


By running test_klass I get a compilation error, newKlasse1 is not
defined.

What is the problem ?

Thanks.

Olivier
 
B

Bob Phillips

Hi Olivier,

I tested it and it all worked fine for me (I had to make some changes to the
code to get it to work)

Private Sub test_klass()
Dim anObj As Klasse1
Set anObj = New Klasse1
Debug.Print "From klasse1 : " & anObj.helloWorld
End

I guess that you are trying to call it from another workbook though and that
iss where the problem may be. You cannot access a class in another workbook,
add-in or not, directly. You have to get at it in a roundabout way.

In the workbook with the class you have to createa a standard code moduke
procedure for doing the business with the class.

Public Sub TestMyClass()
Dim anObj As Klasse1
Set anObj = New Klasse1
Debug.Print "From klasse1 : " & anObj.helloWorld
End

Note that it is Public now.

Then in the calling workbook, you invoke that routine like so

Application.Run "Testing.xla!TestMyClass"

If you need an answer/result, you c an always use a function and run that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top