此 C# 代码位于 .NET 4.5ComVisible
程序集中:
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 32bit 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 中丢失了自动完成(智能感知),这在我的场景中是不可接受的。
我在示例中关心的行是以下行:
oComExposedEarlyBinding.DoubleArray = VBArray
oComExposedEarlyBinding.ObjectArray = VBArray
oComExposedEarlyBinding.PlainObject = VBArray
让上述三行中的任何一行都可以满足我的需求,那么您是否有任何解决方法或解决方案可以使这项工作(请注意,我对将数组作为参数传递给函数不感兴趣)?
更新: 将此问题提交给 Microsoft 的支持并等待近三周后。他们确认这是一个错误,这是 KB: http: //support.microsoft.com/kb/327084,C#中唯一的解决方法是标记为下面的解决方案。但是,如果用 C++/CLI 编写,我可以确认此代码可以按预期工作。