I need create Interop VBA.Collection object from C# code
I have reference to Interop.VBA in my project
我需要从C#代码创建Interop VBA.Collection对象我在我的项目中引用了Interop.VBA
When I'm calling that:
我打电话的时候:
var col = new VBA.Collection()
In runtime I've got an error saying that dll is not registered...
I found that: http://support.microsoft.com/kb/323737/en-us
在运行时我有一个错误说dll没有注册...我发现:http://support.microsoft.com/kb/323737/en-us
It might work but I don't have VB6 compiler on my box.
I wonder you know other workaround (or maybe someone can compile this ActiveX to me?)
它可能会工作,但我的盒子上没有VB6编译器。我想知道你知道其他的解决方法(或者有人可以编译这个ActiveX给我?)
2 个解决方案
#1
4
I haven't tried this, but it might work.
我没试过这个,但它可能会奏效。
Create an import library for VB6's VBA6.dll. Create your own implementation of its _Collection interface. Use this implementation in place of the VBA.Collection class.
为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);
}
}
#2
0
I have adapted this for vb.net, and had to fix the key coming to the add, because it's null when missing.
我已经为vb.net调整了这个,并且必须修复添加的密钥,因为它在丢失时为空。
I will edit this post after I test it. I need to make sure it works when VB6 calls a .Net dll, passing it a vba collection as a parameter, and the .Net dll passes another vba collection back as the return value. Man, if it works this will save me so much trouble!
我测试后会编辑这篇文章。当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
EDIT:
编辑:
No, this does not work with VB6. VB6 says:
不,这不适用于VB6。 VB6说:
"Class does not support Automation or does not support expected interface"
“类不支持自动化或不支持预期的接口”
The class it's talking about here is my class that uses VBACollection instead of VBA.Collection. VBACollection is not an identical stand-in for VBA.Collection. I'd like to find out why and try to fake COM out into accepting it.
它在这里讨论的类是我的类,它使用VBACollection而不是VBA.Collection。 VBACollection与VBA.Collection不是一个相同的替身。我想找出原因并尝试假装COM接受它。
#1
4
I haven't tried this, but it might work.
我没试过这个,但它可能会奏效。
Create an import library for VB6's VBA6.dll. Create your own implementation of its _Collection interface. Use this implementation in place of the VBA.Collection class.
为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);
}
}
#2
0
I have adapted this for vb.net, and had to fix the key coming to the add, because it's null when missing.
我已经为vb.net调整了这个,并且必须修复添加的密钥,因为它在丢失时为空。
I will edit this post after I test it. I need to make sure it works when VB6 calls a .Net dll, passing it a vba collection as a parameter, and the .Net dll passes another vba collection back as the return value. Man, if it works this will save me so much trouble!
我测试后会编辑这篇文章。当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
EDIT:
编辑:
No, this does not work with VB6. VB6 says:
不,这不适用于VB6。 VB6说:
"Class does not support Automation or does not support expected interface"
“类不支持自动化或不支持预期的接口”
The class it's talking about here is my class that uses VBACollection instead of VBA.Collection. VBACollection is not an identical stand-in for VBA.Collection. I'd like to find out why and try to fake COM out into accepting it.
它在这里讨论的类是我的类,它使用VBACollection而不是VBA.Collection。 VBACollection与VBA.Collection不是一个相同的替身。我想找出原因并尝试假装COM接受它。