如何在 C# 中创建 VBA.Collection() 对象

Mac*_*iej 4 .net excel vba c#-4.0

我需要从 C# 代码创建 Interop VBA.Collection 对象
我在我的项目中引用了 Interop.VBA

当我打电话时:

var col = new VBA.Collection()
Run Code Online (Sandbox Code Playgroud)

在运行时我有一个错误说 dll 未注册...
我发现:http : //support.microsoft.com/kb/323737/en-us

它可能会工作,但我的机器上没有 VB6 编译器。
我想知道您知道其他解决方法(或者也许有人可以将这个 ActiveX 编译给我?)

Foo*_*ole 5

我没有试过这个,但它可能会奏效。

为 VB6 的 VBA6.dll 创建一个导入库。创建您自己的 _Collection 接口实现。使用此实现代替 VBA.Collection 类。

class MyCollection : VBA._Collection
{
    private Dictionary<object, object> _items = new Dictionary<object, object>();

    public void Add(ref object Item, [System.Runtime.InteropServices.OptionalAttribute]ref object Key, [System.Runtime.InteropServices.OptionalAttribute]ref object Before, [System.Runtime.InteropServices.OptionalAttribute]ref object After)
    {
        // Ignoring the Before and After params for simplicity
        _items.Add(Key, Item);
    }

    public int Count()
    {
        return _items.Count;
    }

    public System.Collections.IEnumerator GetEnumerator()
    {
        return _items.Values.GetEnumerator();
    }

    public dynamic Item(ref object Index)
    {
        return _items[Index];
    }

    public void Remove(ref object Index)
    {
        _items.Remove(Index);
    }
}
Run Code Online (Sandbox Code Playgroud)