How to unregister a UDF

Mar 12, 2013 at 10:17 PM
Hello,
I need unregister a UDF from an xll based on a setting in my addin not the whole xll.
I see http://msdn.microsoft.com/en-us/library/office/bb687866.aspx
but how to do the same in ExcelDNA using c#?

I see https://groups.google.com/forum/?fromgroups#!topic/exceldna/w_mjHW7D7eI
is to remove the whole xll.

Thanks
Coordinator
Mar 13, 2013 at 11:44 AM
Hi,

This was from an [Excel-DNA Google group post]: (https://groups.google.com/group/exceldna/browse_frm/thread/8225b121c076996d/43fcffc682fe34bd)
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); 
} 
-Govert
Mar 13, 2013 at 5:00 PM
Edited Mar 13, 2013 at 5:47 PM
Thank you very much, Govert. It works!!
Another question, how to register a function
I use the following but it does not work
    public static void RegisterFunction(string name)
    {
        // get the path to the XLL 
        var xllName = XlCall.Excel(XlCall.xlGetName);

        // reregister the function as hidden and then unregister (clears the function wizard) 
        var reregId = XlCall.Excel(XlCall.xlfRegister, xllName, "xlAutoAdd", "I", name, ExcelMissing.Value, 1);
        XlCall.Excel(XlCall.xlfSetName, name);
        XlCall.Excel(XlCall.xlfRegister, reregId);
    }
I am wondering what is right syntax for xlcall.Excel,
I see you have
        var reregId = XlCall.Excel(XlCall.xlfRegister, xllName, "xlAutoRemove",
                                   "I", name, ExcelMissing.Value, 2);
but it does not match the syntax of xlfRegister @ http://msdn.microsoft.com/en-us/library/office/bb687900.aspx?

Thanks
Coordinator
Mar 13, 2013 at 6:54 PM
Hi,

Are you trying to register a different function? You won't be able to use xlfRegister directly - that's where a lot of the Excel-DNA magic comes in to glue .NET and Excel together.

There is a method called ExcelIntegration.RegisterMethods(...) that allow you to register methods at runtime. You should take some care - each call (which can take a list of MethodsInfos) has significant overhead - behind the scenes a new assembly is compiled and loaded. Also, even though the technique posted above will remove the function's name from Excel, the memory and internal Excel-DNA structures are not cleaned up.

The only reliable and safe way to add and remove functions at runtime is to load and unload a separate .xll.

-Govert