Passing User Class to Macro Restricted?

95 views
Skip to first unread message

KCTechPro

unread,
Feb 6, 2018, 11:30:39 AM2/6/18
to Excel-DNA
I'm calling a macro via the event handler.  If I pass a string all is well (MyMethod1).  If I pass a user class (MyMethod2) I'm getting a type exception.  Is this restricted?

System.Reflection.TargetInvocationException

  HResult=0x80131604

  Message=Exception has been thrown by the target of an invocation.

  Source=mscorlib

  StackTrace:

   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)

   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)

   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)

   at ExcelNGN.Main.MyMethod2(IRibbonControl control) in H:\ExcelNGN\ExcelNGN\Main.cs:line 48

 

Inner Exception 1:

COMException: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))


namespace ExcelNGN
{
    [ComVisible(true)]
    public class Main : ExcelRibbon
    {
        public override string GetCustomUI(string RibbonID)
        {
            return @"
      <ribbon>
        <tabs>
          <tab id='tab1' label='MyTab'>
            <group id='group1' label='Pass Data'>
                <button id='Button1' label='Button1' onAction='MyMethod1'/>
                <button id='Button2' label='Button2' onAction='MyMethod2'/>
                <button id='HideMe1' label='Hide Me1' visible='false' onAction='RunTagMacro' tag='MyMacro1'/>
                <button id='HideMe2' label='Hide Me2' visible='false' onAction='RunTagMacro' tag='MyMacro2'/>            </group >
          </tab>
        </tabs>
      </ribbon>
    </customUI>";
        }
        public void MyMethod1(IRibbonControl control)
        {
            MessageBox.Show("MyMethod1 - triggered");
            Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
            xlapp.GetType().InvokeMember("Run", BindingFlags.InvokeMethod,
                null, xlapp, new object[] { "MyMacro1", "MyParm" });
        }
        public static void MyMacro1(string arg1)
        {
            MessageBox.Show(arg1);
        }
        public void MyMethod2(IRibbonControl control)
        {
            MessageBox.Show("MyMethod2 - triggered");
            Mapper xclMapper = new Mapper();
            Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
            xlapp.GetType().InvokeMember("Run", BindingFlags.InvokeMethod,
            null, xlapp, new object[] { "MyMacro2", xclMapper });
        }
                public static void MyMacro2(Mapper arg1)
        {
            Mapper.RowModel parm1 = arg1.RowModels[1];
            MessageBox.Show(parm1.UserLabel);
        }
    }

}


Govert van Drimmelen

unread,
Feb 6, 2018, 12:51:33 PM2/6/18
to exce...@googlegroups.com
The types that Application.Run will support as parameters are limited, and does not include CLR references or general COM objects.

One approach would be to have an internal dictionary that has a key -> object mapping and pass the key to the macro through the Application.Run call.

-Govert


From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of KCTechPro [kcte...@gmail.com]
Sent: 06 February 2018 06:30 PM
To: Excel-DNA
Subject: [ExcelDna] Passing User Class to Macro Restricted?

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at https://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

KCTechPro

unread,
Feb 6, 2018, 1:05:18 PM2/6/18
to Excel-DNA
Thanks Govert!
To post to this group, send email to exc...@googlegroups.com.
Message has been deleted

Виталий Бедный

unread,
Feb 14, 2018, 12:55:35 PM2/14/18
to Excel-DNA
Govert, Great product!
Can you describe this approach in more detail?
How can I transfer an object through COM?
Does your product implement registration of COM without administrator rights?
Through the manifest?
Thank you!

вторник, 6 февраля 2018 г., 20:51:33 UTC+3 пользователь Govert van Drimmelen написал:

Govert van Drimmelen

unread,
Feb 14, 2018, 4:56:51 PM2/14/18
to exce...@googlegroups.com
Here are detailed step-by-step instructions for trying out the COM server support:

It does not require administrator permissions - when run with limited permissions the required registry entries are made in the user hive.

-Govert


From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of Виталий Бедный [bedv...@gmail.com]
Sent: 14 February 2018 07:55 PM
To: Excel-DNA
Subject: Re: [ExcelDna] Passing User Class to Macro Restricted?

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.

Виталий Бедный

unread,
Feb 15, 2018, 2:45:15 AM2/15/18
to Excel-DNA
Thanks Govert!
Good decision. Is this solution not suitable?
+
I wrote my xll on c ++, I do not want to add com, do you know how you can transfer an object via win api or via Application.Run call.
could you tell us more about:
"One approach would be to have an internal dictionary that has a key -> object mapping and pass the key to the macro through the Application.Run call."
+
I apologize for English, this language is not my native language.


четверг, 15 февраля 2018 г., 0:56:51 UTC+3 пользователь Govert van Drimmelen написал:
Message has been deleted

Виталий Бедный

unread,
Feb 15, 2018, 3:01:48 AM2/15/18
to Excel-DNA
For .NET (...a tool to generate Sxs manifest for managed assembly for Registration Free COM/.Net Interop)
https://blogs.msdn.microsoft.com/junfeng/2007/04/14/genman32-a-tool-to-generate-sxs-manifest-for-managed-assembly-for-registration-free-com-net-interop/

четверг, 15 февраля 2018 г., 10:45:15 UTC+3 пользователь Виталий Бедный написал:

Govert van Drimmelen

unread,
Feb 15, 2018, 3:25:47 AM2/15/18
to exce...@googlegroups.com
I was not able to get SxS to work for the COM stuff I wanted to do in Excel - it might work for you.

 Not sure where your 'object' comes from, but maybe you can pass a pointer to the C++ code via Application.Run. Since it's in the same process there should be to problem. Guess it depends what you want to do with it...

-Govert
--

Виталий Бедный (bedvit)

unread,
Feb 15, 2018, 5:53:00 AM2/15/18
to Excel-DNA
I have a class with methods and properties in C ++ (mathematics).
I would like to use it in VBA.
Except for COM I do not see how this can be done.

Govert van Drimmelen

unread,
Feb 15, 2018, 6:24:00 AM2/15/18
to exce...@googlegroups.com
Then I'm not sure why you need .NET (and Excel-DNA) in there too.

-Govert


From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of Виталий Бедный (bedvit) [bedv...@gmail.com]
Sent: 15 February 2018 12:52 PM

To: Excel-DNA
Subject: Re: [ExcelDna] Passing User Class to Macro Restricted?
I have a class with methods and properties in C ++ (mathematics).
I would like to use it in VBA.
Except for COM I do not see how this can be done.

--

Виталий Бедный (bedvit)

unread,
Feb 15, 2018, 9:38:16 AM2/15/18
to Excel-DNA
Wrapped the C ++ class in C # and used your wonderful product.
Now there was time and decided to add the code to C ++.
I saw your phrase "One approach would be to have an internal dictionary that has a key -> object mapping and pass the key to the macro through the Application.Run call" and decided to clarify this possibility and some points in your product.

четверг, 15 февраля 2018 г., 14:24:00 UTC+3 пользователь Govert van Drimmelen написал:
Reply all
Reply to author
Forward
0 new messages