Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to call a VFP COM method from Excel
Message
From
11/02/2003 14:23:05
 
 
To
10/02/2003 22:23:46
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00751581
Message ID:
00751997
Views:
12
Sure, Thanks again. It's a very important thing to keep in mind..


>Juan,
>
>Great! Glad to hear it.
>
>I noticed though that by using the function exactly as I have it, if you have a large number of cells using the same calculation, then performance may slow down considerably because it is creating and destroying all these objects for each calculation (an expensive process in terms of system resources).
>
>One way to minimize this performance hit is to create the object only once and reference it for each calculation. In the following example I would have set a reference to the MyServer.MyClass object in an Excel workbook. I would then add a code module and enter the following code:
>
>
>Public myMult As MyServer.MyClass
>
>Public Function Multiply(intA as Integer, intB as Integer) As Integer
>    If myMult Is Nothing Then
>        Set myMult = New MyServer.MyClass
>    End If
>
>    Multiply = myMult.Multiply(intA, intB)
>End Function
>
>
>In the workbook code window, I would enter the following code to clean up memeory:
>
>
>Private Sub Workbook_BeforeClose(Cancel As Boolean)
>    If Not myMult Is Nothing Then
>        Set myMult = Nothing
>    End If
>
>End Sub
>
>
>I hope this helps with performance.
>
>Don
>
>
>
>>Don, Thanks a lot
>>
>>It worked and the function also appears under User Defined Functions.
>>
>>
>>
>>
>>
>>>Juan,
>>>
>>>Insert a code module into the Excel workbook, and define a Public Function with the proper parameters and enclose code provided previously inside this function and return the value.
>>>
>>>Example:
>>>
>>>Public Function Multiply(intA as Integer, intB as Integer) As Integer
>>>    Set myMult = CreateObject("MyServer.MyClass")
>>>
>>>    Multiply = myMult.Multiply(intA, intB)
>>>
>>>    Set myMult = Nothing
>>>End Function
>>>
>>>
>>>You should then be able to enter into any cell in the workbook "=Multiply(5, 4)" and the result should calculate.
>>>
>>>This function will also appear in the Function Dialog Box under the User Defined category.
>>>
>>>Hope this helps!
>>>
>>>Don
>>>
>>>
>>>>Thanks anyway, but I wanted to see my COM Server Methods as functions under the Insert - Function Menu, Category: MyServer.MyClass.
>>>>
>>>>I compiled the COM project with VFP7, not VFP6, and it's working now, so I can create formulas in excel involving my VFP functions, for example:
>>>>=Multiply(5,4)
>>>>
>>>>
>>>>
>>>>Thanks.
>>>>
>>>>
>>>>
>>>>>Juan,
>>>>>
>>>>>You can just use it in an Excel macro like so:
>>>>>
Set myMult = CreateObject("MyServer.MyClass")
>>>>>MsgBox " 5 times 4 is " & CStr(myMult.Multiply(5,4)), vbOKOnly + vbInformation, "Multiplication"
>>>>>You can also add the VFP component to the VBA project's references to use it early-bound, but this is the simplest method.
>>>>>
>>>>>HTH,
>>>>>
>>>>>>Hi
>>>>>>
>>>>>>I have been reading VFP and Excel help system in order to add optional functions in excel but I can't get sucess at all. This is what I've done:
>>>>>>
>>>>>>1. I Created a Custom Olepublic class with an example Method to multiply numbers. I want further to seek and return values from VFP tables.
>>>>>>
>>>>>>2. In Excel, I went to Tools - Add-Ins - Automation and I selected MyServer.MyClass from the available servers list.
>>>>>>
>>>>>>3. But I get this error:
>>>>>>Cannot find Add-in 'c:\comserver\myserver.exe /automation'. Delete from list ? YES/NO
>>>>>>
>>>>>>
>>>>>>Any specific guidelines, white papers or books to build this VFP server ?
>>>>>>
>>>>>>How to implement help to be displayed when, in Excel, I click on Insert - Function - Category MyServer.MyClass ?
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>Thanks
>>>>>>
>>>>>>Juan
Previous
Reply
Map
View

Click here to load this message in the networking platform