我在我的Excel DNA插件中有以下代码:
在我的AutoOpen方法中,我放置了以下代码:
ExcelIntegration.RegisterUnhandledExceptionHandler(ex => ex.ToString());
我有以下函数,它会从我的Excel表格中调用。
[ExcelFunction(Category = "Foo", Description = "Sets value of cell")]
public static Foo(String idx)
{
Excel.Application app = (Excel.Application)ExcelDnaUtil.Application;
Excel.Workbook wb = app.Workbooks[1];
Excel.Worksheet ws = GetSheet("Main");
// This gives us the row
Excel.Name idxRange = wb.Names.Item("COL_Main_Index");
var row = (int)app.WorksheetFunction.Match(idx, idxRange.RefersToRange, 0);
// Get the Column
Excel.Name buyOrderRange = wb.Names.Item("COL_Main_BuyOrder");
var col = (int)buyOrderRange.RefersToRange.Cells.Column;
// create the range and update it
Excel.Range r = (Excel.Range)ws.Cells[row, col];
r.Value ="Foo";
}
问题是我无法实际设置任何单元格的值。当我调用该方法时,会导致最后一行出现错误。
我的错误处理程序给出了以下错误:
{System.Runtime.InteropServices.COMException (0x800A03EC)
我也尝试这样设置单元格的值:
r = (Excel.Range)ws.Cells[12, 22];
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
r.GetType().InvokeMember("Value2", BindingFlags.SetProperty, null, r, args1);
有相同的结果。
有人能指出我在这里可能做错了什么吗?