none
Calling .NET from VBA RRS feed

  • Question

  • Hey,

    I'm trying to invoke the methods available in the managed component (COM visible) and consume its events. I'm able to pass primitive types (int, bool, etc) via events but not System.EventArgs. Or atleast, is it possible to pass Structures in events?

    The following code shows an EventArgs, a custom EventArgs and a Struct which I'm trying to pass thro' the event.

    [ComVisible(true)]
    public class CalcEventArgs : EventArgs
    {
            public int Operand1 { get; set; }<br/>

            public int Operand2 { get; set; }<br/>

            public string Operation { get; set; }<br/>

            public int Result { get; set; }<br/>
    }

    [ComVisible(true)]
    public struct CalcEventArgsStruct
    {
            public int Operand1 { get; set; }

            public int Operand2 { get; set; }

            public string Operation { get; set; }

            public int Result { get; set; }
    }

    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]<br/>
    public interface IEvents
    {
            [DispId(3)]
            void OnCompleted(object sender, EventArgs e);

            [DispId(4)]
            void OnResult(object sender, CalcEventArgs e);

            [DispId(5)]
            void OnResult(CalcEventArgsStruct result);
    }

    [ComVisible(true)]
    [ComSourceInterfaces(typeof(IEvents))]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [Guid("3D6F8D15-A532-412e-9B60-A5AB4541461F")]
    public class CalcProxy
    {
            ...
    }

    I'm getting an error "Variable uses an Automation type not supported in Visual Basic" with trying to use the events in VBA

    The following is the VBA script which is using the events.

    Public Sub Calculator_OnResult(ByVal result As Math_Wrapper.CalcEventArgsStruct)
        MsgBox result(0) & " " & result(1) & " " & result(2) & " = " & result(3)
        txtZ.Text = result.Operation
    End Sub


    Any ideas to resolve this issue?

    Thanks in advance.

    Param
    Friday, July 17, 2009 12:19 PM

Answers

  • You can't pass structures in an IDispatch based interface, they are not Automation compatible.  Make it a class.  COM also doesn't support function overloads, you should give the 2nd OnResult a different name.

    Hans Passant.
    Friday, July 17, 2009 1:15 PM
    Moderator

All replies

  • You can't pass structures in an IDispatch based interface, they are not Automation compatible.  Make it a class.  COM also doesn't support function overloads, you should give the 2nd OnResult a different name.

    Hans Passant.
    Friday, July 17, 2009 1:15 PM
    Moderator
  • Can I pass a custom EventArgs? When I tried, I get the same "Variable uses an Automation type not supported in Visual Basic" error in VBA.
    Thursday, July 30, 2009 9:36 AM