+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: calling DLL

  1. #1
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    calling DLL

    Hi Guys,

    I have a simpleCalc dll that I want to call. The dll was compiled in C#.

    DLL Code:
    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;
            }
        }
    }
    Then I am trying to reference it in VBA:

    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
    But it errors on objCalc.setNumberOne = 3, saying object doesnt support this property or method.

    Any advice please
    Last edited by Jacques Grobler; 01-03-2012 at 02:06 AM.
    Jacques


  2. #2
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: calling DLL

    Any of the clever people out there to help please...
    Jacques


  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,351

    Re: calling DLL

    Did you include a reference to the dll via VBE menu Tools > References?
    Cheers
    Andy
    www.andypope.info

  4. #4
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: calling DLL

    Hi Andy,

    Yes I have included the reference
    Jacques


  5. #5
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: calling DLL

    Just to bump it up to the top again... :D
    Jacques


  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,351

    Re: calling DLL

    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)
    Cheers
    Andy
    www.andypope.info

  7. #7
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: calling DLL

    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


  8. #8
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: calling DLL

    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:
    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
    and below is the vba code:
    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
    In declaring the new class, I'm not sure what must go where. If someone can please help me out on this...

    Tx in advance
    Jacques


  9. #9
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: calling DLL

    Can't see the Namespace there but I think it would be

    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
    You haven't said what is happening with the current code.
    Good luck.

  10. #10
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: calling DLL

    Hi,

    Why do I need NameSpace in vb.net?

    It gives me an error on the "set" line
    Jacques


  11. #11
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: calling DLL

    In VBA instead of
    Public Function myAddValues(ByVal value1 As Double, ByVal value2 As Double)
      Dim Result As Double
      Result = value1 + value2
      Return Result
    End Function
    I'd write:

    Public Function myAddValues(ByVal value1 As Double, ByVal value2 As Double)
      myAddValues = value1 + value2
    End Function



  12. #12
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: calling DLL

    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


  13. #13
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: calling DLL

    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.

  14. #14
    Valued Forum Contributor
    Join Date
    01-15-2010
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: calling DLL

    I get:

    Run-time error '429':

    ActiveX component can't creat object

    And yes, my DLL is COM registered
    Jacques


  15. #15
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: calling DLL

    Then the error is in your dll code.
    Good luck.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0