1

I succeed to create a ATL library in c++ which does some operation and gives back solutions. So I have all the functions in excel by using automation. Now I would like to create a macro which could do all the functions in the right order and after that maybe associating a button to it. So I've written this macro:

Sub Macro_test()

 Dim r As Integer

 r = setValInput(Range("F21"), Range("G21"))
 r = initializeCalculation()
 Range("C24") = r
 r = getResult("C21")


End Sub

If i build this Excel says the function setValInput isn't definite. The class of ATL is named OPclass, maybe have I to do something with this?

Community
  • 1
  • 1
The Newbie Toad
  • 186
  • 2
  • 15

2 Answers2

3

You create object using CreateObject method. The argument is the ProgID value you provided to C++ ATL wizard when you created the COM class, or/and you can look it up in the project .RGS files.

Code snippet:

   Set MyServer = CreateObject("ProgettoOPserver")
   MyServer.setValInput(Range("F21"), Range("G21"))

Methods and properties on the ATL class exposed through implemented interface will be automatically available to your VBA script code.

Another option is to first add a reference to the external COM server (ATL based library) and instead of CreateObject you would be able to use New operator. See more on this: How to add a reference programmatically. The advantage is that you will be able to see the available methods in Object Browser.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thanks. I have do it, the ProgID is 'ProgettoOPserver' so i have to write CreateObject("ProgettoOPserver") ?? I tried it but continues to say function not definite. Have I to write function with class identifier? In example Web.getResults? – The Newbie Toad Sep 21 '13 at 19:28
  • I update the code snippet. From this end it's well set, further questions are for the other side - the ATL/C++ implementation. – Roman R. Sep 21 '13 at 19:42
0

A method that's a member of a class object needs to be called from an instance of the class object. As you have already created OPClass module which presumably contains the method setValInput, try something like:

Dim op as New OPClass
Debug.Print op.setValInput(Range("F21"),Range("G21"))

This assumes you have elsewhere created the object, per Roman's answer.

David Zemens
  • 53,033
  • 11
  • 81
  • 130