<i id='yTTr1'><tr id='yTTr1'><dt id='yTTr1'><q id='yTTr1'><span id='yTTr1'><b id='yTTr1'><form id='yTTr1'><ins id='yTTr1'></ins><ul id='yTTr1'></ul><sub id='yTTr1'></sub></form><legend id='yTTr1'></legend><bdo id='yTTr1'><pre id='yTTr1'><center id='yTTr1'></center></pre></bdo></b><th id='yTTr1'></th></span></q></dt></tr></i><div id='yTTr1'><tfoot id='yTTr1'></tfoot><dl id='yTTr1'><fieldset id='yTTr1'></fieldset></dl></div>

      • <bdo id='yTTr1'></bdo><ul id='yTTr1'></ul>
    1. <tfoot id='yTTr1'></tfoot>
    2. <small id='yTTr1'></small><noframes id='yTTr1'>

        <legend id='yTTr1'><style id='yTTr1'><dir id='yTTr1'><q id='yTTr1'></q></dir></style></legend>
      1. 使用 COM 互操作将对象从 C# 传递到 VBA

        Passing objects from C# to VBA using COM Interop(使用 COM 互操作将对象从 C# 传递到 VBA)

        1. <small id='rFODL'></small><noframes id='rFODL'>

            <tbody id='rFODL'></tbody>
            • <bdo id='rFODL'></bdo><ul id='rFODL'></ul>
              1. <legend id='rFODL'><style id='rFODL'><dir id='rFODL'><q id='rFODL'></q></dir></style></legend><tfoot id='rFODL'></tfoot>
                • <i id='rFODL'><tr id='rFODL'><dt id='rFODL'><q id='rFODL'><span id='rFODL'><b id='rFODL'><form id='rFODL'><ins id='rFODL'></ins><ul id='rFODL'></ul><sub id='rFODL'></sub></form><legend id='rFODL'></legend><bdo id='rFODL'><pre id='rFODL'><center id='rFODL'></center></pre></bdo></b><th id='rFODL'></th></span></q></dt></tr></i><div id='rFODL'><tfoot id='rFODL'></tfoot><dl id='rFODL'><fieldset id='rFODL'></fieldset></dl></div>

                  本文介绍了使用 COM 互操作将对象从 C# 传递到 VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以使用 COM 将自定义对象(如 MyClass[])从 C# 传递到 VBA?

                  Is it possible to pass a custom object (like MyClass[]) from C# to VBA using COM?

                  如果没有,哪个是最好的解决方案?

                  If not, which is the best solution to get this working?

                  推荐答案

                  我假设你在谈论 Excel VBA 到 C# ...

                  I assume you're talking about Excel VBA to C# ...

                  这是一个最小的 C# 类,在一个默认名称为 ClassLibrary1 的项目中:

                  here's a minimal C# class that does it, in a project w default name ClassLibrary1:

                  using System;
                  using System.Runtime.InteropServices;
                  
                  namespace Tester
                  {
                      [ClassInterface(ClassInterfaceType.AutoDual)]
                      public class TestClass
                      {
                          public double D { get; set; }  // simple property to get, set a double
                          public string S { get; set; }  // simple property to get, set a string
                      }
                  }
                  

                  这里是 VBA 来试试这个类:

                  and here's VBA to try the class out:

                  Private Sub foo()
                  
                  Dim X As New ClassLibrary1.TestClass
                  
                  X.S = "Hello"
                  Debug.Print X.S ' prints "hello"
                  
                  X.D = 12
                  Debug.Print X.D ' prints a 12
                  
                  End Sub
                  

                  要完成这项工作,您还需要做以下额外的事情:

                  and here are the extra things you need to do to make this work:

                  (1) in C# Project...Properties...Build ==> check "Register for COM interop
                  (2) in C# Project...Properties...Application...Assembly Information ==> 
                      check "Make assembly COM-visible"
                  (3) in VBA ... Tools ... References, browse to the C# bin output directory and select the "*.tlb" file
                  

                  注意:此方案可能会失败,具体取决于您添加到类中的内容 - 我认为 VBA 不会看到"静态类或除默认构造函数之外的类 w.您也不能将 VB 集合映射到 .NET 集合,但您将能够来回传递基本类型(double、long)和基本类型的数组.此外 - 使用的Autodual"选项是一种公开方法的廉价方式......易于上手但效率较低,并且公开了所有公共方法.更好的做法(但更多的工作)是设置您自己的接口.如果您扩展此 TestClass 的成员以包含您定义的其他类的实例,并且如果您同样通过 AutoDual 或通过手动编码的接口公开这些类方法,那么这些类及其(非重载)方法同样是可见的在 VBA 中(使用 Intellisense).

                  Note: this scheme may fail depending on what you add to the class - I don't think VBA will "see" static classes or classes w other than default constructors. You also cannot map VB collections to .NET collections, but you will be able to pass basic types (double, long) and arrays of the basic types back and forth. Also - the "Autodual" option used is a cheap way to get methods exposed ... easy to get started but less efficient and exposes all public methods. Better practice (but more work) would be to set up your own interfaces. If you expand the members of this TestClass to include instances of other classes you have defined, and if you likewise expose those class methods via AutoDual or via hand-coded interfaces, then those classes and their (non-overloaded) methods will likewise be visible in VBA (with Intellisense).

                  希望这会有所帮助.

                  这篇关于使用 COM 互操作将对象从 C# 传递到 VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                  <i id='t03K7'><tr id='t03K7'><dt id='t03K7'><q id='t03K7'><span id='t03K7'><b id='t03K7'><form id='t03K7'><ins id='t03K7'></ins><ul id='t03K7'></ul><sub id='t03K7'></sub></form><legend id='t03K7'></legend><bdo id='t03K7'><pre id='t03K7'><center id='t03K7'></center></pre></bdo></b><th id='t03K7'></th></span></q></dt></tr></i><div id='t03K7'><tfoot id='t03K7'></tfoot><dl id='t03K7'><fieldset id='t03K7'></fieldset></dl></div>
                    <tbody id='t03K7'></tbody>
                • <legend id='t03K7'><style id='t03K7'><dir id='t03K7'><q id='t03K7'></q></dir></style></legend>
                    <bdo id='t03K7'></bdo><ul id='t03K7'></ul>

                      <tfoot id='t03K7'></tfoot>
                          • <small id='t03K7'></small><noframes id='t03K7'>