Excel 2010中的64位无法创建.NET对象 [英] Excel 2010 64 bit can't create .net object

查看:196
本文介绍了Excel 2010中的64位无法创建.NET对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在Excel中使用一个简单的类库。这里是我的类的简化...

I have a simple class library that I use in Excel. Here is a simplification of my class...

using System;
using System.Runtime.InteropServices;

namespace SimpleLibrary
{
 [ComVisible(true)]
 public interface ISixGenerator
 {
  int Six();
 }

 public class SixGenerator : ISixGenerator
 {
  public int Six() 
  {
   return 6; 
  }
 }
}

在Excel 2007中我会创造宏启用工作簿,然后添加一个模块下面的代码:

In Excel 2007 I would create a macro enabled workbook and add a module with the following code:

Public Function GetSix()
    Dim lib As SimpleLibrary.SixGenerator
    lib = New SimpleLibrary.SixGenerator
    Six = lib.Six
End Function

然后在Excel中,我可以调用函数GetSix(),它会返回六人。这不再是在Excel 2010工作64位。我得到一个运行时错误'429':ActiveX组件不能创建对象

Then in Excel I could call the function GetSix() and it would return six. This no longer works in Excel 2010 64bit. I get a Run-time error '429': ActiveX component can't create object.

我试图改变目标平台到x64,而不是任何CPU,但随后我的代码不会编译除非我取消选中了注册COM互操作选项,这样使得它让我的宏工作簿能不能看到SimpleLibrary.dll因为它不再regsitered。

I tried changing the platform target to x64 instead of Any CPU but then my code wouldn't compile unless I unchecked the Register for COM interop option, doing so makes it so my macro enable workbook cannot see SimpleLibrary.dll as it is no longer regsitered.

任何想法如何,我可以用我的图书馆与Excel 2010 64位?

Any ideas how I can use my library with Excel 2010 64 bit?

推荐答案

您还没有详细说明如何创建你的.NET程序集。不过,也有揭露大会COM所需的步骤一定数量的:

You haven't described in detail how your created your .NET assembly. However, there are a certain number of steps required to expose the assembly to COM:


  • 添加以下属性到您的代码:

  • Add the following attributes to your code:

using System;
using System.Runtime.InteropServices;


namespace SimpleLibrary
{
    [ComVisible(true)]
    [Guid("71F645D0-AA78-4447-BA26-3A2443FDA691")]
    public interface ISixGenerator
    {
        int Six();
    }


[ComVisible(true)]
[ProgId("SimpleLibrary.SixGenerator")]
[Guid("8D59E0F6-4AE3-4A6C-A4D9-DFE06EC5A514")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class SixGenerator : ISixGenerator
{
    [DispId(1)]
    public int Six()
    {
        return 6;
    }
}



}


  • 您必须装配签署(项目 - >属性 - >签署的,形成强大的关键文件,并检查盒子签署组装

  • Your assembly must be signed (Project -> Properties... -> Signing, create a strong key file and check the box to sign the assembly

    下面的命令是必要的注册程序集(全部在一行中):

    The following command is necessary to register the assembly (all in one line):

    C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe 
                      SimpleLibrary.dll /tlb SimpleLibrary.tlb /codebase
    

    这将创建一个.TLB类型库文件,你会从你的VBA项目引用(工具 - >引用 - >浏览... 的在VBA编辑器)

    This creates a .tlb type library file which you will have to reference from your VBA project (Tools -> References -> Browse... in your VBA editor)

    调整VBA代码:

    Public Function GetSix()
        Dim lib As SimpleLibrary.SixGenerator
        Set lib = New SimpleLibrary.SixGenerator
        GetSix = lib.Six
    End Function
    


  • 你会发现微软的支持数据库在这篇文章中详细描述的步骤:

    You will find the steps described in more detail in this article on Microsoft's support database:

    如何调用一个Visual Basic .NET或Visual Basic 2005组件从Visual Basic 6.0

    How to call a Visual Basic .NET or Visual Basic 2005 assembly from Visual Basic 6.0

    这篇关于Excel 2010中的64位无法创建.NET对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆