none
problem passing structure from VB.Net (VSTO AddIn) to VBA RRS feed

  • Question

  • I've successfully passed arrays of String and arrays of Long, so I have the basic mechanics of the COM interface for passing data from VB back to VBA working. (Most of those details are left out of fragments that follow.) But I have a structure() on the VB side that I'd like to pass back to VBA and receive as an array of user defined type; I've tried all manner of permutations and haven't found one that works. Here's logically what I'm trying to make work:

    VB side:

    Public Structure OpResult
        Public ReadOnly Property Result As ResultsEnum
        Public ReadOnly Property Context As String
        Public Sub New(Result As ResultsEnum, Optional Context As String = Nothing)
            Me.Result = Result
            Me.Context = Context
        End Sub
    End Structure
    ...
    Public Interface IMyAppdata
        ReadOnly Property OpResults As OpResult()
    End Interface
    ...
        Private ReadOnly Property IMyAppdata_OpResults As OpResult() Implements IMyAppdata.OpResults
            Get
                ' convert the Queue(Of OpResult) to an Array
                IMyAppdata_OpResults = OpResults.ToArray
            End Get
        End Property

    VBA side:

    Type OpResult
        Result As Long
        Context As String
    End Type
    
        Dim results() As OpResult
        results = myAddInObject.OpResults

    As shown here, I get a Type mismatch error on the VBA assignment to results. ? typename(myAddInObject.OpResults) yields Array. The value of MyAddInObject,Opresult is a string "OpResult[]". So it, perhaps naively, seems like I'm somehow close to making it work.

    As I said above, I've tried countless variations on how to declare this both on the VB side and on the VBA side (Object, Variant, (), not (), ...) but not found a combination that works. (Typically, either I get compile problems on one side or the other or VBA throws Object required or Invalid procedure call or argument errors.)

    Also, as I said above, I've successfully decomposed the array of structures on the VB side and passed back its elements as separate () Of Long and () Of String, so the basic mechanics for passing data are in place and working.

    I've also read lots of Google results, but haven't found my solution. I'm wondering if the correct <StructLayoutAttribute()> might be part of my solution (triggered from this article), but, absent an understanding of why, exactly, I'm failing, I haven't gone there yet.

    Are there any VB (not C#) examples of this kind of thing? Any suggestions? Surely this can be made to work, right?


    • Edited by Dick Watson Tuesday, December 11, 2018 5:39 PM
    Tuesday, December 11, 2018 5:37 PM

All replies

  • Hi,

    According to your description, your issue is more related about VSTO . And this forum is discussing and asking questions about the vb.net.

    Ask in the following forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto

    Thank you for your understanding.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 12, 2018 6:24 AM
  • I'm not sure there's any magic to VSTO's involvement. The question is fundamentally about passing a structure (Queue(Of Structure)) from Managed to Unmanaged code. Besides, I asked the question on the VSTO forum and got no helpful answers...
    Tuesday, December 18, 2018 3:55 PM
  • Hi,

    I  am sorry that this forum only discusses and ask vb.net, does not discuss and ask VSTO.

    Thank you for your understanding.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 19, 2018 9:11 AM
  • I'm sorry I mentioned VSTO in the first place since it isn't central to the question I posed. If I said I don't want to discuss VSTO, just how to declare a structure like mine in VB if you are going to pass it to a COM object, would you answer the question?
    Wednesday, December 19, 2018 3:22 PM