Accessing .NET Shared Objects from VBA

My .net code has an object with a number of common properties. This object is returned in VBA code. All non-generic properties work well, but I also need to access the generic values. Is there any way to do this from VBA?

[ClassInterface(ClassInterfaceType.AutoDual)]
public class Obj
{
    public string GetProp1() {...}
    public IList<MyCustomType> GetProp2() {...}
}

VB Code:

Sub Test()
    Dim o As Program.Obj
    Set o = New Program.Obj
    Set p2 = hp.GetProp2()

    set p2_0 = p2(0) ' doesn't work

End Sub
+3
source share
2 answers

I found this article describing an ugly hack - naming everything through reflection. Since the overall value is still returned as an object, the reflection should work, but will be 1) slow, 2) error prone and 3) very inconvenient to call.

As shown in the article, I would use utilities with the following signatures:

public object GetProperty(object Instance, string Property)
public object InvokeMethod(object Instance, string Method)
public object InvokeMethod_OneParm(object Instance, string Method, object Parm1)
public object InvokeMethod_TwoParms(object Instance, string Method, object Parm1, object Parm2)
public void SetProperty(object Instance, string Property, object Value)

, .

0

( VS COM, , : " " NS.Obj.get_GetProp2 (# 1), Assy '. : exporter . COM.

, , COM ( , ). typelib VBA VBA, - :

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Class1
{
    public IMyListOfString Strings {get; set;}
    public Class1()
    {
        Strings = new MyListOfString() { "Foo", "Bar", "Baz" };
    }
}

[ComDefaultInterface(typeof(IMyListOfString))]
public class MyListOfString : List<string>, IMyListOfString { }

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IMyListOfString
{
    [DispId(0)]
    string this[int idx] { get; set; }
}

, , typelib VBA (, ) , COM, , .

+4

Source: https://habr.com/ru/post/1718497/


All Articles