[英]c# ExcelDNA Passing User Class to Macro Restricted?
我正在使用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);
}
}
將Govert的答案復制到Excel-DNA郵件列表中,以供通過搜索引擎找到的人使用:
作為參數的
Application.Run
將支持的類型是有限的,並且不包括CLR引用或常規COM對象。一種方法是擁有一個內部字典,該字典具有一個鍵->對象映射,並將該鍵通過
Application.Run
調用傳遞給宏。-Govert
https://groups.google.com/forum/#!topic/exceldna/p-LIG-RvUgA
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.