在VBA中无法访问COM显示的方法 [英] Unable to access COM exposed methods in VBA

查看:235
本文介绍了在VBA中无法访问COM显示的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



问题:我看到所有的默认方法在VBA中的C> GetHashCode , GetType ToString em>那些是COM接口的一部分,并且具体写为COM可见(如下面 getStringValue())。



设置详细信息:




  • Visual Studio 2008

  • Windows 7 x64

  • Office 2007

  • .NET 3.5



Interface'IGetMyString .cs'



 使用System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

命名空间SimpleCOMAssembly
{
[ComVisible(true),GuidAttribute(4153A1AC-ECE9-4f66-B56C-1DDEB6514D5D)]
[InterfaceType(ComInterfaceType.InterfaceIsDual )]
interface IGetMyString
{
[DispId(1)]
string getStringValue();
}
}



Implementation'GetMyString.cs'



 使用System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

命名空间SimpleCOMAssembly
{
[ComVisible(true),GuidAttribute(0A3D4D65-CF50-4020-BF13-77001F8AAABE)]
[ProgId(SimpleCOMAssembly。 GetMyString)]
[ClassInterface(ClassInterfaceType.None)]
public class GetMyString:IGetMyString
{
public GetMyString(){}

[ComVisible (true),说明(Get my string)]
public string getStringValue()
{
returnhello;
}
}
}

使装配COM可见(请参见下面的快照)



要求Visual Studio 2005使Register for COM interop(注册为COM互操作)请参阅下面的快照)



/ p>

然后,最后作为后构建事件,我运行regasm.exe注册.DLL和.TLB注册表如下:

 %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\ regasm / codebase$(TargetPath)/ tlb: $(TargetDir)$(TargetName).lib



在Excel对象资源管理器视图中, (SimpleCOMAssembly如上所述),现在在对象资源管理器中没有列出COM接口方法(见下文)



有人可以帮我知道我缺少什么,导致COM接口方法在VBA中不显示?



EDIT
附加ITypeLib视图生成的TLB

解决方案

Excel对象浏览器屏幕截图清楚地显示了一个问题。注意GetHashcode,GetType和ToString方法是如何可见的。这些是继承自System.Object的方法。但是你的代码段显式地(并且正确地)使用了[ClassInterface(ClassInterfaceType.None)],这样类的实现就被隐藏了。



不太确定如何发生,一个旧类型的图书馆从早期的尝试可以解释它。但是你的构建步骤是非常可疑的,你帮助太多。 使汇编类型COM可见选项是强制构建系统公开.NET类型的一种非常粗略的方法。但你的代码是使用精致的右上 - 粉红 - 当你喝茶的方式来暴露类型到COM。其中包括[ComVisible(true)]属性,复选框是什么,以及[ClassInterface]属性,这是复选框不做的。



问题是你要求构建系统实现两个接口。无论它从基类继承,_Object在你的case,加上它继承自声明,IMyGetString在你的case。这是罚款和所有相当COM兼容,但VBA不是一个很好的COM消费者。它只喜欢[Default]接口,那是_Object在你的case。



因此,请关闭Make assembly COM visible选项。



在调用Regasm的postbuild事件或Register for COM interop复选框之间。做这两种方式只是加倍的赔率,你不知道为什么它不工作。当您需要注册64位版本的Office的程序集时,您只需要postbuild。


I am trying to access COM exposed methods in VBA.

Problem: I am seeing all default methods (like GetHashCode, GetType and ToString) in VBA but not the ones which are part of the COM interface and specifically written to be COM visible (like getStringValue() below).

Set up Details:

  • Visual Studio 2008
  • Windows 7 x64
  • Office 2007
  • .NET 3.5

Interface 'IGetMyString.cs'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace SimpleCOMAssembly
{
    [ComVisible(true), GuidAttribute("4153A1AC-ECE9-4f66-B56C-1DDEB6514D5D")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    interface IGetMyString
    {
        [DispId(1)]
        string getStringValue();
    }
}

Implementation 'GetMyString.cs'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace SimpleCOMAssembly
{
    [ComVisible(true), GuidAttribute("0A3D4D65-CF50-4020-BF13-77001F8AAABE")]
    [ProgId("SimpleCOMAssembly.GetMyString")]
    [ClassInterface(ClassInterfaceType.None)]
    public class GetMyString : IGetMyString
    {
        public GetMyString() { }

        [ComVisible(true), Description("Get my string")]
        public string getStringValue()
        {
            return "hello";
        }
    }
}

In Build properties, I checked 'Make Assembly COM Visible' (see below snapshot)

Also asked Visual Studio 2005 to make the 'Register for COM interop' (see below snapshot)

And, finally as a post build event, I am running the regasm.exe to register the .DLL and also the .TLB to registry as follows:

%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\regasm /codebase "$(TargetPath)" /tlb:"$(TargetDir)$(TargetName).lib"

In Excel Object Explorer view, I enabled COM server (SimpleCOMAssembly written above), and now in the Object Explorer it does not list down the COM interface method (see below)

Can someone help me know what I am missing which is causing the COM interface methods not show in VBA?

EDIT Attaching ITypeLib View of the generated TLB

解决方案

The Excel object browser screenshot clearly shows a problem. Note how the GetHashcode, GetType and ToString methods are visible. Those are methods that are inherited from System.Object. But your snippet explicitly (and correctly) uses [ClassInterface(ClassInterfaceType.None)] so that the class implementation is hidden.

It isn't hidden. Not so sure how that happened, an old type library from an earlier attempt could explain it. But your build steps are very suspect, you are helping too much. The "Make assembly types COM visible" option is a pretty crude way to force the build system to expose .NET types. But your code is using the refined upper-right-pinky-up-when-you-drink-tea way to expose types to COM. Which includes the [ComVisible(true)] attribute, what the checkbox does, and the [ClassInterface] attribute, which is what the checkbox doesn't do.

So the problem is that you asked the build system to implement two interfaces. Whatever it inherited from the base class, _Object in your case, plus what it inherited from the declaration, IMyGetString in your case. Which is fine and all quite COM compatible, but VBA isn't a very good COM consumer. It only likes the [Default] interface and that's _Object in your case. Clearly visible from the screenshot.

So turn off the "Make assembly COM visible" option.

And choose between either a postbuild event that calls Regasm or the "Register for COM interop" checkbox. Doing it both ways just doubles the odds that you don't know why it doesn't work. You only need the postbuild when you need to register the assembly for the 64-bit version of Office.

这篇关于在VBA中无法访问COM显示的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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