使用excel addin向UDF注册参数描述

我有一个带有UDF getRegExResult的插件.我想为这个函数添加一个函数描述和参数描述,所以当用户安装addin,关闭,打开excel几次并转到“插入函数”对话框时,他将能够找到带有参数描述的函数.

同样被问到here.我找到了一个适合我需要的answer.除了…

我希望能够通过Excel Addin完成此操作.我的想法是将调用放入addin workbook_open事件,如下所示:

Private Sub Workbook_Open()
    Call getRegExResultRegister
End Sub  

Public Sub getRegExResultRegister()
    Application.MacroOptions Macro:="getRegExResult", Description:="Returns a concatenated string of NONE, ONE, or ALL Regular Expression Match(es).", Category:="User Defined", _
        ArgumentDescriptions:=Array("Source string to inspect for matches.", _
        "Regular Expression Pattern. E.g. ""\d+"" matches at least 1 or more digits.", _
        "[Default = True] True = Returns all the matches found. False = Returns only the first match.", _
        "[Default = True] True = Not case sensitive search. False = Case sensitive search.", _
        "[Default = "";""] Delimiter to insert between every macth, if more than 1 matches are found.")
End Sub

安装插件后,关闭,打开excel,我得到运行时错误1004:“无法在隐藏的工作簿上编辑宏.请在工作簿中找到…”

问题1

如何取消隐藏插件工作簿?我试图在调用注册之前将Thisworkbook.Windows(1).visible = True放入Workbook_open事件中,但这导致Runtime 9,下标超出范围.

问题2

如果unhide addin是不可能的,还有其他方法吗?

感谢帮助.

类似的问题:
Excel Register UDF in Personal.xslb

编辑#1

当前代码做了我想要的,有一个bug.当我打开一些现有的工作簿时,我得到2个excel窗口.其中一个打开的工作簿(正确),其中一个插件(不想要).如何摆脱第二个窗口?

Private Sub Workbook_Open()
    With ThisWorkbook
        .IsAddin = False
        Call getRegExResultRegister
        .IsAddin = True
        .Saved = True
    End With
End Sub

最佳答案 在设置.MacroOption之前,请使用以下代码:

Application.AddIns("Your Addin name").Installed = True

此代码可能需要在:

Application.AddIns("Your Addin name").Installed = False

根据MSDN Blog,这是因为自动加载的AddIns在启动时并未真正打开.所以你必须在重新打开它之前关闭它.

请注意,“您的Addin名称”不是AddIn的文件名,而是它在加载项选项窗口中显示的名称.

编辑:完整代码,不要忘记编辑AddIn名称

Public Sub getRegExResultRegister()
    Application.AddIns("Your Addin name").Installed = False
    Application.AddIns("Your Addin name").Installed = True
    Application.MacroOptions Macro:="getRegExResult", Description:="Returns a concatenated string of NONE, ONE, or ALL Regular Expression Match(es).", Category:="User Defined", _
        ArgumentDescriptions:=Array("Source string to inspect for matches.", _
        "Regular Expression Pattern. E.g. ""\d+"" matches at least 1 or more digits.", _
        "[Default = True] True = Returns all the matches found. False = Returns only the first match.", _
        "[Default = True] True = Not case sensitive search. False = Case sensitive search.", _
        "[Default = "";""] Delimiter to insert between every macth, if more than 1 matches are found.")
End Sub
点赞