我使用ExcelDNA构建一个功能区按钮,该按钮将检索SQL数据,然后通过事件处理程序调用宏来填充新工作表。如果我传递一个字符串一切都很好(MyMethod1)。如果我传递用户类(MyMethod2),我会遇到类型不匹配异常。通过事件处理程序限制传递用户类?
[ComVisible(true)]
public class Main : ExcelRibbon
{
public override string GetCustomUI(string RibbonID)
{
return @"
<customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
<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);
}
}
答案 0 :(得分:1)
在Excel-DNA邮件列表中为通过搜索引擎找到此内容的人复制Govert的answer:
Application.Run
作为参数支持的类型是有限的, 并且不包括CLR引用或一般COM对象。一种方法是拥有一个具有密钥的内部字典 - &gt; 对象映射并通过键将密钥传递给宏
Application.Run
致电。-Govert
https://groups.google.com/forum/#!topic/exceldna/p-LIG-RvUgA