C#COM DLL与Office 2010 64位

时间:2013-04-01 18:16:47

标签: c# com ms-office

我可能会重新发布,但我找不到解决方法。

我创建了一个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无法创建对象。

我想到的唯一原因是我使用Windows 7 64位运行Office 2010 64位。

1 个答案:

答案 0 :(得分:2)

  

然后我选中了Register for Interop选项

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

另一个改进是显式使用[ProgId]属性,因此您不必猜测名称,如果项目名称不是“COMTrial”,则不会出现问题。