Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. HowTo make a dll, which can be called by VBA

HowTo make a dll, which can be called by VBA

Scheduled Pinned Locked Moved C#
csharpquestion
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Ariadne

    Hi, are there any roules for a dll written in C#, which can be called by a VBA Macro? Private Declare Function foo Lib "foo.dll" (sfoo as string) as string How does the C# code for such dll look like?

    Ariadne

    M Offline
    M Offline
    mav northwind
    wrote on last edited by
    #2

    Hi! You'll have to create a COM object for this, VBA cannot access a function in a .NET assembly.

    Regards, mav -- Black holes are the places where God divided by 0...

    1 Reply Last reply
    0
    • A Ariadne

      Hi, are there any roules for a dll written in C#, which can be called by a VBA Macro? Private Declare Function foo Lib "foo.dll" (sfoo as string) as string How does the C# code for such dll look like?

      Ariadne

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #3

      What Mav said is true. But as an addition, you can write your code as C# and make your C# dll, then register it as COM dll then use it from your VBA macro. I think this post[^] would be helpful.

      Regards:rose:

      A 2 Replies Last reply
      0
      • N Nader Elshehabi

        What Mav said is true. But as an addition, you can write your code as C# and make your C# dll, then register it as COM dll then use it from your VBA macro. I think this post[^] would be helpful.

        Regards:rose:

        A Offline
        A Offline
        Ariadne
        wrote on last edited by
        #4

        Really? Is it complicate to register a dll as com?

        Ariadne

        N E 2 Replies Last reply
        0
        • A Ariadne

          Really? Is it complicate to register a dll as com?

          Ariadne

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #5

          Well, not really. You can follow the link to the post I gave you in the previous reply. Here is the link again[^].

          Regards:rose:

          1 Reply Last reply
          0
          • N Nader Elshehabi

            What Mav said is true. But as an addition, you can write your code as C# and make your C# dll, then register it as COM dll then use it from your VBA macro. I think this post[^] would be helpful.

            Regards:rose:

            A Offline
            A Offline
            Ariadne
            wrote on last edited by
            #6

            Thanks. If I use tlbexp I get only my class exposed, but not my methods. Any ideas? (I suppose, the methods have to have a certain format.) Nevertheless I do not belive, that a COM-object is the only way to get a connection to VBA. With a COM-object you do not need a Declare Function statement. But with a Declare Function one can access all routines eg. in the kernel.dll. (And that are not COM's in kernel.dll).

            Ariadne

            N A 2 Replies Last reply
            0
            • A Ariadne

              Thanks. If I use tlbexp I get only my class exposed, but not my methods. Any ideas? (I suppose, the methods have to have a certain format.) Nevertheless I do not belive, that a COM-object is the only way to get a connection to VBA. With a COM-object you do not need a Declare Function statement. But with a Declare Function one can access all routines eg. in the kernel.dll. (And that are not COM's in kernel.dll).

              Ariadne

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #7

              If you got your class exposed, how come not the methods in it?? Aren't they public? If there is some sort of a VBA .Net language then this would be a solution to your problem, bur otherwise there is no other direct conection between umanaged-to-managed code that I know of.

              Regards:rose:

              1 Reply Last reply
              0
              • A Ariadne

                Thanks. If I use tlbexp I get only my class exposed, but not my methods. Any ideas? (I suppose, the methods have to have a certain format.) Nevertheless I do not belive, that a COM-object is the only way to get a connection to VBA. With a COM-object you do not need a Declare Function statement. But with a Declare Function one can access all routines eg. in the kernel.dll. (And that are not COM's in kernel.dll).

                Ariadne

                A Offline
                A Offline
                Ariadne
                wrote on last edited by
                #8

                public class PW { public static string Convert5Bit(byte[] abytID) { //do somthing here ... return sConv; } } This is the declaration. But only PW as class is exposed. -- modified at 5:19 Thursday 26th October, 2006

                Ariadne

                N S 2 Replies Last reply
                0
                • A Ariadne

                  public class PW { public static string Convert5Bit(byte[] abytID) { //do somthing here ... return sConv; } } This is the declaration. But only PW as class is exposed. -- modified at 5:19 Thursday 26th October, 2006

                  Ariadne

                  N Offline
                  N Offline
                  Nader Elshehabi
                  wrote on last edited by
                  #9

                  First: Post your question to my answers, not to your question. I only saw your question by chance. Second: Try to reference to your new COM object in a C++ or C# project and see what is/isn't exposed Third: If none other works -I'm not that expert in VBA so if applicable- Make your method non-static, and call it form an instance of the class rather than the calss itself. Ps. BTW, what way did you know by it's not exposed?

                  Regards:rose:

                  A 1 Reply Last reply
                  0
                  • A Ariadne

                    public class PW { public static string Convert5Bit(byte[] abytID) { //do somthing here ... return sConv; } } This is the declaration. But only PW as class is exposed. -- modified at 5:19 Thursday 26th October, 2006

                    Ariadne

                    S Offline
                    S Offline
                    SSLaks
                    wrote on last edited by
                    #10

                    VBA does not support static functions. Also, you are confusing Declare with COM. To get at an exposed COM object in VBA, ad a reference to it, then instansiate the class. Declare Function has nothing to do with COM - it uses access points, which cannot be created from C#. To create an access point, use an MFC dll. Schabse S. Laks

                    1 Reply Last reply
                    0
                    • N Nader Elshehabi

                      First: Post your question to my answers, not to your question. I only saw your question by chance. Second: Try to reference to your new COM object in a C++ or C# project and see what is/isn't exposed Third: If none other works -I'm not that expert in VBA so if applicable- Make your method non-static, and call it form an instance of the class rather than the calss itself. Ps. BTW, what way did you know by it's not exposed?

                      Regards:rose:

                      A Offline
                      A Offline
                      Ariadne
                      wrote on last edited by
                      #11

                      thanks for 1st. 2nd I look into the code with notepad and try in VBA the Object-Browser. both show only PW and not Convert5Bit() 3rd it tried that, but no success. thanks for your ansers! :rose:

                      Ariadne

                      N 1 Reply Last reply
                      0
                      • A Ariadne

                        Really? Is it complicate to register a dll as com?

                        Ariadne

                        E Offline
                        E Offline
                        ednrgc
                        wrote on last edited by
                        #12

                        you should check : ServicedComponent

                        1 Reply Last reply
                        0
                        • A Ariadne

                          thanks for 1st. 2nd I look into the code with notepad and try in VBA the Object-Browser. both show only PW and not Convert5Bit() 3rd it tried that, but no success. thanks for your ansers! :rose:

                          Ariadne

                          N Offline
                          N Offline
                          Nader Elshehabi
                          wrote on last edited by
                          #13

                          Ariadne wrote:

                          Object-Browser. both show only PW and not Convert5Bit()

                          Well, in that object browser the method should be under the class name, shouldn't it? I mean you access the method by the class name? Also what file did you look into using the notepad?

                          Ariadne wrote:

                          3rd it tried that, but no success.

                          Define "no success"? You can't instantiate the object from the exposed class? Or the instance can't access the member function?

                          Regards:rose:

                          A 1 Reply Last reply
                          0
                          • N Nader Elshehabi

                            Ariadne wrote:

                            Object-Browser. both show only PW and not Convert5Bit()

                            Well, in that object browser the method should be under the class name, shouldn't it? I mean you access the method by the class name? Also what file did you look into using the notepad?

                            Ariadne wrote:

                            3rd it tried that, but no success.

                            Define "no success"? You can't instantiate the object from the exposed class? Or the instance can't access the member function?

                            Regards:rose:

                            A Offline
                            A Offline
                            Ariadne
                            wrote on last edited by
                            #14

                            Nader Elshehabi wrote:

                            Also what file did you look into using the notepad?

                            I opened the tlb-File with notepad: There was no Convert5Bit.

                            Nader Elshehabi wrote:

                            object browser the method should be under the class name

                            Under the class name is no method. If I instantiate the class I get an runtime error: 429 ActiveX component can't create object. But VBA knows the class PW.

                            Ariadne

                            N A 2 Replies Last reply
                            0
                            • A Ariadne

                              Nader Elshehabi wrote:

                              Also what file did you look into using the notepad?

                              I opened the tlb-File with notepad: There was no Convert5Bit.

                              Nader Elshehabi wrote:

                              object browser the method should be under the class name

                              Under the class name is no method. If I instantiate the class I get an runtime error: 429 ActiveX component can't create object. But VBA knows the class PW.

                              Ariadne

                              N Offline
                              N Offline
                              Nader Elshehabi
                              wrote on last edited by
                              #15

                              Well, I haven't tested the issue thoroughly. I did some googling and found this post on MSDN forum Post link[^] and here are two more articles in MSDN about the issue: Article 1[^] Article 2[^] Also I did what I told you. I made a tlb from the .Net dll, and it actually references to the class and the methods inside the class!! About the instantiation errer; Did you make your constructor public? I hope this directs you somewhere. Good luck! And I'm waiting for your feedback.

                              Regards:rose:

                              A 1 Reply Last reply
                              0
                              • N Nader Elshehabi

                                Well, I haven't tested the issue thoroughly. I did some googling and found this post on MSDN forum Post link[^] and here are two more articles in MSDN about the issue: Article 1[^] Article 2[^] Also I did what I told you. I made a tlb from the .Net dll, and it actually references to the class and the methods inside the class!! About the instantiation errer; Did you make your constructor public? I hope this directs you somewhere. Good luck! And I'm waiting for your feedback.

                                Regards:rose:

                                A Offline
                                A Offline
                                Ariadne
                                wrote on last edited by
                                #16

                                Thanks for your articles. I read a lot of stuff too.:omg: But it seams there is something basic wrong. here the story, what I try: 1. I build a classlibrary-project in VS8 C#. 2. I insert one TestDLL class: using System; using System.Collections.Generic; using System.Text; public class TestDLL { public string StringChange(string str) { return str +" from C#"; } } 3. I build the project with a TestDLL.dll as result 4. I run tlbexp TestDLL.dll /win 32 /out:TestDLL.tlb 5. An inspection of TestDLL.tlb with notepad.exe shows no StringChange string. 6. I run regasm TestDLL.dll It returns a waring: RA00000 : there are no types registered (retranslation from german) 7. I tested the same on a virgin VS8 Enviroment on my home notebook with the same result. 8. I have no idea....:(

                                Ariadne

                                1 Reply Last reply
                                0
                                • A Ariadne

                                  Nader Elshehabi wrote:

                                  Also what file did you look into using the notepad?

                                  I opened the tlb-File with notepad: There was no Convert5Bit.

                                  Nader Elshehabi wrote:

                                  object browser the method should be under the class name

                                  Under the class name is no method. If I instantiate the class I get an runtime error: 429 ActiveX component can't create object. But VBA knows the class PW.

                                  Ariadne

                                  A Offline
                                  A Offline
                                  Ariadne
                                  wrote on last edited by
                                  #17

                                  That's it: in assemblyinfo.cs there is a switch: [assembly: ComVisible(true)] but it was false Now all works fine...:laugh: Thank you for your support Nader

                                  Ariadne

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups