从Excel-DNA中的工具栏激活写入当前单元格

时间:2015-06-02 08:25:25

标签: c# excel-dna

我有一个从工具栏上的按钮运行的功能。

该按钮加载一个窗体,允许用户执行产品查找并返回产品代码:

功能区:

          <button id='btnProductLookup' label='Product Code Lookup' image='ProdCode' size='large' onAction='ProductCodeLookup'/>

方法:

    public void ProductCodeLookup(IRibbonControl control)
    {
        using (FrmProductLookup lookup = new FrmProductLookup(_callHandler))
        {
            DialogResult result = lookup.ShowDialog();
            if (result == DialogResult.OK)
            {
                ExcelReference current = XlCall.Excel(XlCall.xlfActiveCell) as ExcelReference;
                MessageBox.Show(current.ColumnFirst.ToString());
                MessageBox.Show(lookup.SelectedProductCode);
            }

        }
    }

我想将lookup.SelectedProductCode中的值写入当前选定的单元格,但每次都会出现异常。

1 个答案:

答案 0 :(得分:2)

C API(XlCall.Excel)在功能区处理程序中不能直接使用。

您可以使用ExcelAsyncUtil.QueueAsMacro(...)转换到宏上下文,也可以在此设置中使用COM对象模型。