此C#代碼是在.NET 4.5 ComVisible
組件:暴露於VBA(COM)C#屬性:運行時錯誤 '424':對象需要
C#代碼
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("22341123-9264-12AB-C1A4-B4F112014C31")]
public interface IComExposed
{
double[] DoubleArray { get; set; }
object[] ObjectArray { get; set; }
object PlainObject { get; set; }
double ScalarDouble { get; set; }
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("E4F27EA4-1932-2186-1234-111CF2722C42")]
[ProgId("ComExposed")]
public class ComExposed : IComExposed
{
public double[] DoubleArray { get; set; }
public object[] ObjectArray { get; set; }
public object PlainObject { get; set; }
public double ScalarDouble { get; set; }
}
從Excel 2010中32位VBA,我有以下行爲:
VBA代碼
Dim VBArray(1 To 3) As Double
VBArray(1) = 1
VBArray(2) = 2
VBArray(3) = 3
Dim oComExposedEarlyBinding As New ComExposed
' Works
oComExposedEarlyBinding.ScalarDouble = 5
' Compile Error: Function or interface marked as restricted,
' or the function uses an Automation type not supported in Visual Basic
oComExposedEarlyBinding.DoubleArray = VBArray
' Compile Error: Function or interface marked as restricted,
' or the function uses an Automation type not supported in Visual Basic
oComExposedEarlyBinding.ObjectArray = VBArray
' Run-time error '424': Object required
oComExposedEarlyBinding.PlainObject = VBArray
' Run-time error '424': Object required
oComExposedEarlyBinding.PlainObject = 5
Dim oComExposedLateBinding As Variant
Set oComExposedLateBinding = New ComExposed
' Works
oComExposedLateBinding.ScalarDouble = 5
' Run-time error '5': Invalid procedure call or argument
oComExposedLateBinding.DoubleArray = VBArray
' Run-time error '13': Type mismatch
oComExposedLateBinding.ObjectArray = VBArray
' Works
oComExposedLateBinding.PlainObject = VBArray
' Works
oComExposedLateBinding.PlainObject = 5
正如您所注意到的PlainObject
正在後期綁定模式下工作,但很顯然,這是由於輸入丟失而導致VBA中的自動完成(IntelliSense)失敗,這在我的場景中是不可接受的。
,我關心的皺紋在我的例子是以下行:
oComExposedEarlyBinding.DoubleArray = VBArray
oComExposedEarlyBinding.ObjectArray = VBArray
oComExposedEarlyBinding.PlainObject = VBArray
得到任何的三條線以上工作會滿足我的需要,所以你有什麼解決方法或解決方案,這將使這項工作(請注意,我不想將數組作爲參數傳遞給函數)?
更新: 將此問題提交給Microsoft的支持並等待將近三週後。他們確認這是一個bug,這是KB:http://support.microsoft.com/kb/327084,C#中唯一的解決方法就是標記爲下面的解決方案。 但是,如果用C++/CLI編寫,我能夠確認此代碼的工作方式與預期的相同。
甲VBA陣列必須0下界是與C#陣列兼容。 –
我一直在將數組作爲函數參數從COM傳遞給.NET,然後將它們轉換爲.NET中的基於零的數據。你是說因爲這個問題上面的代碼失敗了嗎? – Adam