如何在Excel VBA中使用c#类? [英] How to use c# class in Excel VBA?

查看:173
本文介绍了如何在Excel VBA中使用c#类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独立的c#应用程序执行某些特定的操作(侦听TCP端口,并通过语音合成器发出到达它的所有字符串)。如何使c#类对VBA程序可见,与其他引用可见一样?我会喜欢简短而干净的例子。我很难找到其中一个,因为某些原因。



如果有一些特定于c#< - > vba交互的问题,我也想知道这些。



这是一个C#代码。我建立的是一个类库,具有注册COM互操作设置。
当我将生成的.tlb文件添加到VBA引用列表中时,我希望看到有SayCom类的SayCom类有2个方法,square和getCount。我没看到我缺少什么?

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

[assembly:CLSCompliant(true)]

命名空间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;
}
}
}


解决方案

这对我有用

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

命名空间ClassLibrary1
{
[Guid(BBF87E31-77E2-46B6-8093-1689A144BFC6)]
[ComVisible(true)]
public界面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;
}
}
}


I have a standalone c# applications that does something specific (listens to TCP port and pronounces all strings that arrive to it via speech synthesizer). How can I make the c# class visible to a VBA program, same way other "References" are visible to it? I would appreciate short and clean example. I struggle to find one of those for some reason.

If there are some gotchas specific to c# <-> vba interaction, I would like to know about those too.

Here is a C# code. I build is as a class library with "Register for COM interop" setting. When I add the resulting .tlb file to VBA references list, I expect to see SayCom library that has SayCom class with 2 methods, square and getCount. I do not see that. 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;
            }
        }
    }

解决方案

This works for me

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;
            }
    }
}

这篇关于如何在Excel VBA中使用c#类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆