【发布时间】:2011-08-19 14:14:52
【问题描述】:
我是使用 C# 编写插件的新手(对于 C# 也是如此)。我已经看到 ExcelDNA 非常适合简单的函数,但我遇到了一个简单的任务:操作范围(特别是通过它们的名称来解决)。您会看到我尝试将 Interop 用于此任务,这对我来说看起来很简单。在这段非常简单的代码中我做错了什么?我已经去过 ExcelDNA 谷歌小组,但我发现的答案很尴尬
提前非常感谢
使用 ExcelDna.Integration;
namespace MyAddIn
{
public class MyClass {
[ExcelFunction(Category = "MyFunctions", IsMacroType = true)]
public static string MyMacro(int a, int b)
{
var app = (Microsoft.Office.Interop.Excel.Application) ExcelDnaUtil.Application;
var range = app.get_Range("MyTag"); // this line does not fail, but I don't know if it
// selects the right range -- although MyTag exists
range.set_Value("hello") // it fails here
return (string) range.Value2; // or it would fail here as well
}
}
}
它会抛出这个错误
“System.Reflection.TargetInvocationException”类型的第一次机会异常发生在 mscorlib.dll 中 mscorlib.dll 中发生了“System.Runtime.InteropServices.COMException”类型的第一次机会异常
【问题讨论】: