.NET的DLL在VBA(EXCEL) [英] .NET DLL in VBA (Excel)

查看:162
本文介绍了.NET的DLL在VBA(EXCEL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了最后2天试图让创建在VBA工作基本VB.NET DLL。我已阅读本视线的每一个职位,但我不断收到同样的错误。




  1. 我创建了一个VB.NET类(我运行Visual工作室以管理员身份)。



我班

 进口系统
进口System.Collections.Generic
进口System.Text
进口System.Runtime.Interposervices
进口System.Linq的

公共类识别TestClass
功能TestMethod的(BYVAL输入为String)
返回你好和安培;输入
端功能
端类




  • 我设置使装配自带可见

  • 我设置注册为COM互操作

  • 我建立和释放DLL


  • 我打开Excel和添加到DLL的引用。




  • 一切伟大的工程,到目前为止




  • 在一个片我添加了下面的代码

     公用Sub测试()
    暗淡了作为识别TestClass注:自动填写一旦我开始在这么键入它我知道,有美元DLL b $ b将一个=新的TestClass
    MSGBOX(a.TestMethod(世界)
    端子




  • 当我尝试运行代码我得到以下错误



    < BLOCKQUOTE>

    ActiveX组件不能创建对象




    我曾尝试使用这两台电脑:Win7的64位Excel 2010中的64位,VS 2010和Win7 64位时,Excel 2013,Visual Studio 2010的64位与知道运气。我看过的人收到同样的错误,但我似乎没有任何工作。



    有谁看到我的方法的任何错误


    解决方案

    ? - 添加命名空间到你的代码。结果
    -make功能为公共。结果
    -make其COM可见,并设置注册为COM Interop你所说的结果
    - 你的代码应该是这样的:

     命名空间X 
    酒店的公共类识别TestClass
    公共功能TestMethod的(BYVAL输入作为字符串)
    返回你好和安培;输入
    端功能
    端类
    端命名空间



    -build你的项目,你会发现在\bin\debug或\bin\release一个yourProjName.tlb文件。结果
    -open Excel和添加到yourProjName.tlb的引用不DLL。< BR>
    -modify你的代码是这样的:

     子测试()
    昏暗testObj作为新TestClass的
    昏暗myStr中作为字符串
    myStr中= testObj.TestMethod(SSSSSS)
    MSGBOX myStr的
    端子

    这为我工作。结果
    修改#1 结果
    -I我在Windows 7上工作32位的Office 2010专业版32位SP2中,Visual Studio 2010中使用框架4.结果
    -configure项目针对x64:从Visual Studio - >转到生成菜单 - >单击配置管理器 - >在活动解决方案平台,单击新建,然后添加一个带有64位的平台。利用这个平台来编译针对x64,链接。结果
    - 它是首选签署程序集(很容易):项目属性>签名>登录装配>新建 - >输入文件myKey.snk,不需要密码的名称
    -build项目结果
    - 如果你在同一台机器上工作,在运行Visual Studio,则无需注册程序集,因为VS会做,因为我们设置'注册COM互操作结果
    - 对于其他客户机在哪里VS在未运行,您必须注册组装,只需要的DLL文件,运行CMD以管理员身份运行以下命令:结果

      C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /代码库/证:D:\\ \\out\VB_DLL_001.dll

    请注意,这个命令会为您生成* .tlb文件在同一个文件夹中。结果
    -add你的DLL到全局程序集缓存通过在cmd中运行以下命令(即以管理员身份运行):

     C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\gacutil.exe/我D:\out\VB_DLL_001.dll

    - 从Excel中添加引用生成的TLB文件:VBA编辑器 - >工具 - >引用 - >浏览 - >选择在TLB文件 - >确定。然后运行上面的代码,我希望它会成功运行。搜索结果
    修改#2基于注释结果
    ,它通过构建DLL对于 64 架构,因此,没有必要在EDIT 1#其他步骤。


    I have spent the last 2 days trying to get a basic VB.NET dll that created to work in VBA. I have read every single post on this sight but i keep getting the same error.

    1. I create a VB.NET class (I run Visual Studio's as administrator).

    My class

    Imports System
    Imports System.Collections.Generic
    Imports System.Text  
    Imports System.Runtime.Interposervices
    Imports System.Linq
    
    Public Class TestClass
    Function TestMethod(ByVal input as String)
        Return "Hello" & input
    End Function
    End Class
    

    1. I set "Make assembly comes visible"
    2. I set "Register for COM interop"
    3. I build and release the dll

    4. I open Excel and add a reference to the dll.

    Everything works great so far

    1. In a sheet I add the below code

      Public Sub test()
          Dim a As TestClass 'Note: Auto fills in once i start typing it in so i know that the DLL there 
          Set a = New TestClass  
          MsgBox (a.TestMethod("World")
      End Sub
      

    When i try running the code I get the following error

    "ActiveX component can't create object"

    I have tried this using 2 computers: Win7 64 bit, Excel 2010 64 bit ,VS 2010 and Win7 64 bit, Excel 2013 , VS 2010 64 bit with know luck. I have read people receiving the same error but I nothing seems to work.

    Does anyone see any mistakes in my method?

    解决方案

    -add a namespace to your code.
    -make the function as public.
    -make it com visible and set 'Register for COM interop' as you mentioned.
    -your code should be like:

    Namespace X
        Public Class TestClass
            Public Function TestMethod(ByVal input As String)
                Return "Hello" & input
            End Function
        End Class
    End Namespace
    

    -build your project, you will find a yourProjName.tlb file in \bin\debug or \bin\release.
    -open Excel and add a reference to the yourProjName.tlb not to dll.
    -modify your code to be like:

    Sub test()
        Dim testObj As New TestClass
        Dim myStr As String
        myStr = testObj.TestMethod("ssssss")
        MsgBox myStr
    End Sub
    

    This worked for me.
    EDIT #1
    -I am working on Windows 7 32 bit, Office 2010 professional 32 bit with SP2, Visual Studio 2010 with framework 4.
    -configure your project for x64 : from Visual Studio ->Go to the Build Menu ->click Configuration Manager -> Under Active solution platform, click New, then add one with x64 as the platform. Use this platform to compile for x64 , link .
    -It is preferred to sign your assembly (very easy) : Project Properties > Signing > Sign the assembly > New -> Enter the name of the file as myKey.snk , no need for password.
    -build your project.
    - if you are working on the same machine that visual studio in running, then no need to register your assembly, because VS will do since we set 'Register for COM interop'.
    -for other client machines where VS in not running, you must register your assembly, take the DLL file only, run cmd as administrator, run the following command:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /codebase /tlb "D:\out\VB_DLL_001.dll"
    

    Note that this command will generate the *.tlb file for you in the same folder.
    -add your dll to Global Assembly cache by running the following command in cmd (that is running as administrator) :

    "c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\gacutil.exe" /i "d:\out\VB_DLL_001.dll"
    

    -from Excel add reference to the generated tlb file : VBA Editor -> Tools -> References -> Browse -> Select the tlb file -> OK . Then run the previous code, I hope it will run successfully .

    EDIT #2
    Based on the comment, It works by building the DLL for x64 architecture, So, no need for other steps in EDIT #1.

    这篇关于.NET的DLL在VBA(EXCEL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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