Hi Guys,
I have a simpleCalc dll that I want to call. The dll was compiled in C#.
DLL Code:
Then I am trying to reference it in VBA:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace simpleCalc { public class Calc { private int numberOne = 0; private int numberTwo = 0; public void SetNumberOne(int number) { numberOne = number; } public void SetNumberTwo(int number) { numberTwo = number; } // Add two numbers public int Add() { return numberOne + numberTwo; } } }
But it errors on objCalc.setNumberOne = 3, saying object doesnt support this property or method.Option Explicit Public Function test() Dim lngResult As Long Dim objCalc As simpleCalc.Calc Set objCalc = New simpleCalc.Calc objCalc.setNumberOne = 3 objCalc.setNumberTwo = 9 lngResult = objCalc.Add() End Function
Any advice please
Last edited by Jacques Grobler; 01-03-2012 at 02:06 AM.
Jacques
Any of the clever people out there to help please...
Jacques
Did you include a reference to the dll via VBE menu Tools > References?
Hi Andy,
Yes I have included the reference
Jacques
Just to bump it up to the top again... :D
Jacques
You would have had a much speedy solution if you had included a link to the article where the example came from.
http://www.geeksengine.com/article/create-dll.html
You will see that you did not follow the example fully. SetNumberOne and Two are in effect sub routines and not properties. The method of calling them is not via assignment as you have but by passing them as arguments as per the example.
objCalc.setNumberOne (3) objCalc.setNumberTwo (9)
Hi Andy,
Best for the new year
Thanks for the help, yip, might not have had a problem if I just read properly, but thanks anyway.
Jacques
Hi guys,
I'm not usre if I should call this one solved. I have written a basic add function in VB.Net and exported it to a DLL that is COM visible.
I have referenced the dll in vba, however I'm not sure of how to use it in vba.
The dll name is myCOMClassDLL
The class name is myFunctionsCass
And the rest is as below:
Below is the .Net code:
and below is the vba code:Public Class myFunctions Public Function myAddValues(ByVal value1 As Double, ByVal value2 As Double) Dim Result As Double Result = value1 + value2 Return Result End Function End Class
In declaring the new class, I'm not sure what must go where. If someone can please help me out on this...Option Explicit Public Function test() Dim answer As Double Dim objAdd As myCOMClassDLL.myFunctionClass Set objAdd = New myCOMClassDLL.myFunctionClass answer = objAdd.myaddvalues(10, 15) MsgBox answer End Function
Tx in advance
Jacques
Can't see the Namespace there but I think it would be
You haven't said what is happening with the current code.Option Explicit Public Function test() Dim answer As Double Dim objAdd As myCOMClassDLL.myFunctions Set objAdd = New myCOMClassDLL.myFunctions answer = objAdd.myaddvalues(10, 15) MsgBox answer End Function
Good luck.
Hi,
Why do I need NameSpace in vb.net?
It gives me an error on the "set" line
Jacques
In VBA instead of
I'd write:Public Function myAddValues(ByVal value1 As Double, ByVal value2 As Double) Dim Result As Double Result = value1 + value2 Return Result End Function
Public Function myAddValues(ByVal value1 As Double, ByVal value2 As Double) myAddValues = value1 + value2 End Function
Hi snb,
The myAddValues function is based in the VB.Net class file, not in vba.
Do I apply your change to the .net file?
Jacques
I think you should use Namespaces in all .Net languages.
Anyway, what error are you getting? (I assume you registered your dll for COM interop?)
Good luck.
I get:
Run-time error '429':
ActiveX component can't creat object
And yes, my DLL is COM registered
Jacques
Then the error is in your dll code.
Good luck.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks