2010-12-15 120 views
3

我需要从C#代码创建的Interop VBA.Collection对象
我在我的项目有参考Interop.VBA如何创建VBA.Collection()对象在C#

当我打电话说:

var col = new VBA.Collection() 

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

它可能工作,但我没有在我的箱子VB6编译器。
我不知道你知道其他的解决方法(或者也许有人可以编译这个ActiveX给我?)

+1

该页面显示“只有Visual Basic 6.0应用程序才能创建VBA.Collection类的实例”。你需要收集什么?也许还有另一种方法可以做你想要达到的目标。 – Foole 2010-12-15 09:36:40

+0

我正在使用为VBA编写的DLL。需要管理的财产之一什么是收集类型 – Maciej 2010-12-15 18:17:28

回答

4

我还没有尝试过,但它可能工作。

为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); 
    } 
} 
+0

你是男人!作为一种伤害工作! Add()中的小改变Key和Item参数将被替换(意味着1st是Key) – Maciej 2010-12-17 02:19:06

0

我已经适应了这个vb.net,并且必须修复添加的密钥,因为它在失踪时为空。

我会在测试后编辑这篇文章。我需要确保它在VB6调用.Net dll时将其工作,并将它作为参数传递给vba集合,然后.Net dll将另一个vba集合作为返回值传回。男人,如果它有效,这将节省我很多麻烦!

Public Class VBACollection 
    Implements VBA._Collection 

    Private _items As New Dictionary(Of Object, Object) 

    Public Sub Add(ByRef Item As Object, Optional ByRef Key As Object = Nothing, Optional ByRef Before As Object = Nothing, Optional ByRef After As Object = Nothing) Implements VBA._Collection.Add 
    ' Ignoring the Before and After params for simplicity 
    Key = If(Key, Item) 
    _items.Add(Key, Item) 
    End Sub 

    Public Function Count() As Integer Implements VBA._Collection.Count 
    Return _items.Count 
    End Function 

    Public Function GetEnumerator() As System.Collections.IEnumerator Implements VBA._Collection.GetEnumerator, System.Collections.IEnumerable.GetEnumerator 
    Return _items.Values.GetEnumerator() 
    End Function 

    Public Function Item(ByRef Index As Object) As Object Implements VBA._Collection.Item 
    Return _items(Index) 
    End Function 

    Public Sub Remove(ByRef Index As Object) Implements VBA._Collection.Remove 
    _items.Remove(Index) 
    End Sub 
End Class 

编辑:

没有,这不符合VB6工作。 VB6说:

“类不支持自动化或不支持预期 接口”

它谈论的是使用VBACollection代替VBA.Collection我的类的类。对于VBA.Collection,VBACollection不是相同的替代品。我想知道为什么,并试图让COM接受它。