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

时间:2022-12-29 01:12:59

This is my c# library I from which dll is generated, Also have enabled the COM visiblity in Property -> App,Build

这是我的c#库I,从它生成dll,也在属性中启用了COM visiblity——>应用,Build

    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 + "]";
            }
        }
    }

I am trying to access this dll in MS excel using vba as active X

我正在尝试使用vba作为活动X访问MS excel中的这个dll

I added the reference of the dll to this vba code,then created a button and this is the macro definition

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

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

My problem is,

我的问题是,

Here the first access of the project howto_dll_for_excel automatically lists the CSharpTools class,

在这里,项目howto_dll_for_excel的第一个访问将自动列出CSharpTools类,

but in the last CreateObject Statement ,it is not listing,even if I type it manually prompts error

但是在最后的CreateObject语句中,它没有列出,即使我输入它也会提示错误

"Compile error: Method or data member not found" .

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

1 个解决方案

#1


1  

The real problem was that, the dll was 32 bit and the excel was 64 bit version

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

#1


1  

The real problem was that, the dll was 32 bit and the excel was 64 bit version

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