0

Im developing a COM object in C#, VS 2010, .Net 3.5, x86

I used to have a array of structs in the COM Object, which in VBA showed up fine with all the fields and everything.

I switched to class since It created some issues. Now how ever I cant access the properties in the array, since the elements in the array show up as object instead of type.

[Guid("8b65079f-5d98-41e7-9579-1ee384948e4c")]
[ComVisible(true)]
public interface IContact
{
    string Test1 { get; set; }
    string[] Array1 { get; set; }
}

[Guid("8b65089f-5d98-41e7-9579-1ee384948e4c")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Contact : IContact //Used To Be a struct
{
    //[MarshalAs(UnmanagedType.BStr)]
    public string Test1 { get; set; }
    public string[] Array1 { get; set; }
}

public class InContainer
{

        public Contact[] Contacts { get;set;}
        public string[] strings { get; set; }
}

In the debugger I see when viewing the field:

Container.Contacts() -> (0 To 4) As Object

instead of

Container.Contacts() -> (0 To 4) As Contact

What am i missing? Thanks!

4
  • That´s one of the caveats when working with COM, you don´t see what´s inside. Btw.: why is this question tagged C#? Commented Dec 5, 2016 at 15:14
  • The com object is written in C# But I saw what was inside when it was a struct, and I have no issue with other classes Commented Dec 5, 2016 at 15:18
  • I think even in VBA you can use typeof to test the object type. Might be worth a go. Commented Dec 5, 2016 at 16:24
  • Can you show your VBA code? Commented Dec 6, 2016 at 9:46

1 Answer 1

2

Here are some things you can check:

  1. Make sure your container class is correctly declared (I guess it is already COM-visible, but that's not shown in your sample:

    [Guid("EA34C9D6-3EAA-4D44-A8BA-81CC2E79090B")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class InContainer
    
  2. Correctly register your assembly using the correct version of regasm (probably you need the 32-bit one) with the /codebase switch:

    regasm /codebase <myassembly.dll>
    
  3. In VBA, instantiate your container, e.g. using late-binding:

    Sub Test()
        Dim a As Object
        Set a = CreateObject("Issue40977311.InContainer")
    End Sub
    

    This results in the correct types being shown:

    enter image description here

    Alternatively, you can also create a type library using the /tlb switch of regasm:

    regasm /codebase <myassembly.dll> /tlb
    

    and the add the reference to the created .tlb file in VBA via Tools > References > Browse. You can then instantiate your object as follows:

    Sub Test()
        Dim a As Object
        Set a = New Issue40977311.InContainer
    End Sub
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.