我嘗試使用C#ArrayList返回COM對象(用C#開發)的列表,並在VBA中使用此COM對象列表(Excel 2007,.NET 4)vba中的COM對象的ArrayList返回n項<無變量>
我有3個C#函數我從Excel調用(VSTO項目)
public ArrayList GetSimpleArray()
{
ArrayList arr = new ArrayList();
arr.Add(3);
arr.Add(2);
return arr;
}
public ArrayList GetComplexArray()
{
ArrayList arr = new ArrayList();
arr.Add(new Fund() { Id = "2" });
arr.Add(new Fund() { Id = "3" });
return arr;
}
public IFund GetComplexObject()
{
return new Fund() { Id = "2" };
}
與基金和iFund中定義:
[ComVisible(true)]
public interface IFund
{
string Id { get; set; }
string Name { get; set; }
}
[ComVisible(false)]
public sealed class Fund:IFund
{
public string Id { get; set; }
public string Name { get; set; }
public Fund()
{
}
public Fund(string id, string name)
{
this.Id = id;
this.Name = name;
}
}
在Excel我有:
Public Sub GetComObject()
Call InitVSTOAddIn ' Initialise automationObject
Dim complexObject
Set complexObject = automationObject.GetComplexObject()
Dim simpleArray
Set simpleArray = automationObject.GetSimpleArray()
Dim complexArray
Set complexArray = automationObject.GetComplexArray()
End Sub
complexObject和simpleArray具有我正在查找的值(complexObject是具有Id和Name的對象,simpleArray是具有2個元素「3」和「2」的ArrayList)。
問題出在complexArray上。這是與2項的ArrayList,但每一個項目是「不變量」(見圖片附後)
你知道爲什麼嗎?
有沒有辦法在C#中返回一個COM對象列表並在VBA中使用它們?
是不是很好的做法是做這樣的事情還是有更好的方法來使用VBA中的C#對象?
感謝您的幫助
您是否嘗試過爲複雜的ArrayList設置ComVisible爲true? – 2012-07-06 12:10:27
GetComplexObject()返回一個(comvisible)IFund,GetComplexArray不返回基金對象的可見。你爲什麼把基金看作是可觀的(假的)? – Eddy 2012-07-06 12:42:01
我希望只有接口IFund可見COM。它在我返回complexObject時起作用。爲了確保我試圖製作基金ComVisible,但它仍然不起作用。 – Gutti 2012-07-06 12:56:03