如何以编程方式删除Excel UDF

时间:2013-03-11 16:09:28

标签: excel add-in user-defined-functions excel-dna

我有一个旧的Excel插件(用VBA编写),现在我用.NET,ExcelDNA,NetOffice重新编写旧的插件。在旧的插件中,有一个UDF,比如OldUDF,在新的插件中,有一个NewUDF,两个UDF做同样的事情并接受相同的参数。但它们有不同的功能名称。 由于遗留原因,一些客户有旧的插件,一些客户有新的插件。 有些客户同时拥有。 当他们同时安装了Excel插件时,我想提供一个复选框,在新插件中将oldUDF转发到newUDF。选中该复选框后,oldUDF将作为newUDF转发。如果未选中该复选框,则不会转发oldUDF。 我定义了oldUDF&新的插件中的newUDF,但是当取消选中该复选框时,不知道如何删除oldUDF的definiatino。谢谢

编辑:

这是我尝试过的,但它没有用。

#if FORWARD
        public static object OldUDF([ExcelArgument(AllowReference = true, Name = "Symbol",
            Description = "is a futures symbol as defined in *** Database.")]string symbol,
            [ExcelArgument(AllowReference = true, Name = "Column",
                Description = "is a column associated with a symbol")]string column,
            [ExcelArgument(AllowReference = true, Name="Date",
                Description = "is the date for which you want the value")]object onDate,
                object fillOpt, object frequency)
        {
            return NewUDF(symbol, column, onDate, fillOpt, frequency);
        }
#endif 

在AutoOpen()中, 如果forward为true,我会从文件中读取正向值(true或false),

        var forward = Helper.ReadForwardOldUDFSettingFromAFile();
        if(forward)
        {
             #define FORWARD
        }

但是我收到编译错误

再次编辑: 我成功注册了 我把oldUDF的声明放在一个单独的dll中,比如oldUDF.dll 创建另一个oldUDF.xll,oldUDF.dna。

在AutoOpen中,我将检查转发值(来自文件)是否为真,我会这样做 Excel.Application.RegisterXLL( “olderUDF.xll”); 这很好用。

但是,如果转发值为false,我找不到在C#中取消注册或删除XLL的方法?

编辑:我找到http://msdn.microsoft.com/en-us/library/office/bb687866.aspx,但不确定如何在C#

中使用它

谢谢

1 个答案:

答案 0 :(得分:0)

谢谢Govert。他回答了另一组的问题。有用! http://exceldna.codeplex.com/discussions/436393

private static void UnregisterFunction(string name) 
{ 
    // get the path to the XLL 
    var xllName = XlCall.Excel(XlCall.xlGetName); 

    // get the registered ID for this function and unregister 
    var regId = XlCall.Excel(XlCall.xlfEvaluate, name); 
    XlCall.Excel(XlCall.xlfSetName, name); 
    XlCall.Excel(XlCall.xlfUnregister, regId); 

    // reregister the function as hidden and then unregister (clears the function wizard) 
    var reregId = XlCall.Excel(XlCall.xlfRegister, xllName, "xlAutoRemove", 
    "I", name, ExcelMissing.Value, 2); 
    XlCall.Excel(XlCall.xlfSetName, name); 
    XlCall.Excel(XlCall.xlfUnregister, reregId); 
}