How to use C # class in Excel VBA?

I have standalone C # applications that do something specific (listens to the TCP port and pronounces all the lines that come to it through the speech synthesizer). How can I make a C # class visible to a VBA program, just like other "Links"? I would appreciate a short and clean example. I'm struggling to find one of them.

If there are some errors related to C # ↔ vba interaction, I would also like to know about it.

Here is the C # code. I am creating a class library with the setting "Registration for COM interoperability". When I add the resulting .tlb file to the VBA reference list, I expect to see the SayCom library, which has a SayCom class with two methods, a square and getCount. I do not see this. What am I missing?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    [assembly: CLSCompliant(true)]

    namespace SayCom
    {
        [CLSCompliant(true)]
        [ComVisible(true)]
        public class SayCom
        {
            int count;

            public SayCom()
            {
                count = 0;
            }

            [ComVisible(true)]
            public int square(int x)
            {
                ++count;
                return x * x;
            }

            [ComVisible(true)]
            public int getCount()
            {
                return count;
            }
        }
    }
+3
2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ClassLibrary1
{
    [Guid("BBF87E31-77E2-46B6-8093-1689A144BFC6")]
    [ComVisible(true)]
    public interface MyMiniSubs
    {
        int square(int x);
        int getCount();
    }

    [Guid("BBF87E31-77E2-46B6-8093-1689A144BFC7")]
    [ClassInterface(ClassInterfaceType.None)]
    public class Class1 : MyMiniSubs
    {

        int count;

        public Class1()
            {
                count = 0;
            }

            [ComVisible(true)]
            public int square(int x)
            {
                ++count;
                return x * x;
            }

            [ComVisible(true)]
            public int getCount()
            {
                return count;
            }
    }
}

enter image description here

+3

"gotcha", , , # CLS. , # .dll VB. : .

EDIT: MSDN, .dll.

0

All Articles