从MS Excel vba代码调用c#dll - 编译错误:找不到方法或数据成员

时间:2015-03-26 04:55:47

标签: c# excel vba excel-vba dll

这是我生成dll的c#库,也启用了Property中的COM visiblity - >的应用程序,生成

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

    namespace howto_dll_for_excel
    {
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        [ProgId("howto_dll_for_excel.CSharpTools")]
        public class CSharpTools
        {
            [ComVisible(true)]
            public string AddBrackets(string value)
            {
                return "[" + value + "]";
            }
        }
    }

我正在尝试使用vba作为活动X

访问MS excel中的此dll

我在这个vba代码中添加了dll的引用,然后创建了一个按钮,这是宏定义

Sub Button1_Click()
Dim sheet As Worksheet
Dim tools As howto_dll_for_excel.CSharpTools
Dim value As String
Dim result As String
Set sheet = ActiveSheet
value = sheet.Cells(1, 1)
tools = CreateObject("howto_dll_for_excel.CSharpTools")
End Sub

我的问题是,

这里首次访问项目howto_dll_for_excel会自动列出CSharpTools类,

但是在最后一个CreateObject语句中,它没有列出,即使我手动输入提示错误

"编译错误:未找到方法或数据成员" 。

1 个答案:

答案 0 :(得分:1)

真正的问题是,dll是32位而excel是64位版本