-->

Register UDF with descriptions of arguments using

2019-02-24 09:58发布

问题:

I have an addin with an UDF getRegExResult. I want to add a function description and arguments descriptions to this function, so when user installs the addin, closes, opens excel few times and goes to "Insert Function" Dialog box he will be able to find the function with description of the arguments.

The same is asked here. I found one answer that suits my needs. Except...

I want to be able to do this through an Excel Addin. My idea is to put call into addin workbook_open event like so:

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

After I install the addin, close, open excel, I get runtime error 1004: "Cannot edit a macro on a hidden workbook. Uhnide the workbook..."

Question 1

How to unhide an addin workbook? I tried to put Thisworkbook.Windows(1).visible = True into the Workbook_open event before the call to register, but that results in Runtime 9, subscript out of range.

Question 2

If the unhide addin is impossible, is there any other way to do this?


Thanks for help.

Similar questions:
Excel Register UDF in Personal.xslb

Edit #1

Current code does what I want, with one bug. When I open some existing workbook, I get 2 excel windows. One of the opened workbook (correct), one of the addin (not wanted). How to get rid of the second window?

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

回答1:

Use the following code before setting the .MacroOption:

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

This code may need to be preceded by :

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

According to MSDN Blog, it is because automation loaded AddIns are not really opened at startup. So you have to close it before re-openning it.

Note that "Your Addin name" is not the filename of the AddIn but its Name as it appears in the Add-ins option windows.

Edit: Full code, don't forget to edit the AddIn name

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