C# COM DLL with Office 2010 64 bit

本文关键字:2010 bit Office with COM DLL | 更新日期: 2023-09-27 18:07:08

我可能会转发,但我找不到解决办法。

我创建了一个c# Comvisible类。这是下面的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace COMTrial
{
    [Guid("2B71BC1B-16F5-4A0D-A015-CAE658A10B07")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IMyExample
    {
        string GetData();
    }
    [ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(IMyExample))]
    [Guid("2B71BC1B-16F5-4A0D-A015-CAE658A01B07")]
    [ComVisible(true)]
    public class Class1
    {
        public Class1()
        {
        }
        [ComVisible(true)]
        public string GetData()
        {
            return "Vikas";
        }
    }
}

然后我检查Register for Interop选项,甚至使完整的汇编可见,并编译项目和解决方案。

然后我打开excel,写了下面的代码:

Dim a as Object
set a = CreateObject("COMTrial.Class1")

,

ActiveX无法创建对象。

我能想到的唯一原因是我用的是64位的Office 2010和64位的Windows 7。

C# COM DLL with Office 2010 64 bit

然后我检查Register for Interop option

将只为32位进程注册程序集。由于这是64位版本的Office,您需要手动运行Regasm.exe。从Visual Studio命令提示符开始,以"以管理员身份运行"开始。请确保使用64位版本的Regasm.exe,对于。net 4,它默认位于C:'Windows'Microsoft.NET'Framework64'v4.0.30319。注意64。使用/tlb和/cobase选项来匹配IDE的行为。

另一个改进是显式地使用[ProgId]属性,这样您就不必猜测名称,如果项目名称不是"COMTrial",也不会有问题。