Search code examples
exceladd-inuser-defined-functionsexcel-dna

How to remove an Excel UDF programmatically


I have an old Excel addin (written in VBA), now I re-write the old addin using .NET, ExcelDNA, NetOffice. In the old addin, there is a UDF, say, OldUDF, in the new addin, there is a NewUDF, the two UDFs do the same thing and accept the same parameters. but they have different function names. For legacy reason, some clients have old addin, some clients have new addin. Some client have both. When they have both Excel addin installed, I want to provide a checkbox which says forward oldUDF to newUDF in the new addin. When the checkbox is checked, oldUDF is forwarded as newUDF. When the checkbox is not checked, oldUDF will not be forwarded. I define the oldUDF & newUDF in the new addin but do not know how to remove the definiatino of the oldUDF when the checkbox is unchecked. Thanks

Edit:

Here is what I tried, but it is not working.

#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 

In AutoOpen(), I will read the forward value (true or false) from a file, if forward is true,

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

but I get a compile error

Edit again: I managed to register successfully I put declaration of OldUDF in a seperate dll, say oldUDF.dll create another oldUDF.xll, oldUDF.dna.

In AutoOpen, I will check if the forward value (from a file) is true, I will do Excel.Application.RegisterXLL("olderUDF.xll"); This works fine.

However, in case of the forward value is false, I can't find a way to un-register or remove the XLL in C#?

Edit: I found http://msdn.microsoft.com/en-us/library/office/bb687866.aspx, but not sure how to use it in C#

Thanks


Solution

  • Thanks, Govert. He answered the question from the other group. It works! 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); 
    }