【问题标题】:Calling c# dll from MS Excel vba code - Compile error: Method or data member not found从 MS Excel vba 代码调用 c# dll - 编译错误:找不到方法或数据成员
【发布时间】:2015-03-26 04:55:46
【问题描述】:

这是我从中生成 dll 的 c# 库,同时在 Property -> App,Build 中启用了 COM 可见性

    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 Statement中,并没有列出,即使我手动输入也会提示错误

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

【问题讨论】:

  • 据我记忆,CreateObject 需要一个字符串作为输入,试试 CreateObject("howto_dll_for_excel.CSharpTools")
  • 是的,他错过了报价。
  • 感谢您的回复,但这也不起作用
  • @keerthee 添加引号后你得到相同的错误还是不同的错误?
  • 检查以下注册表项是否存在:HKEY_CLASSES_ROOT\howto_dll_for_excel.CSharpTools。如果没有,那么您将不得不注册您的 DLL:regsvr32 c:\path.to\mydll.dll 然后再试一次

标签: c# vba excel dll


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多