Hi Farhan,
For your AutoOpen, I guess you could subscribe to the Workbook-related
events on the Application object (
http://msdn.microsoft.com/en-us/
library/microsoft.office.interop.excel.application_events). Then when
the NewWorkbook event fires, you can set up your names.
But this is getting mighty complicated, compared to just defining a
bunch of functions that return those constants. I'd suggest instead of
the Names you define functions like this in your add-in:
public static double ycFOO() { return 1234; }
Then your user can enter =FancyFunction(ycFoo(), ycBarOptionX(), $A$3)
which is not too bad, except for the extra brackets. (You'll have to
do some parameter validation, since using =ycFoo without the brackets
will return the registration id for the function.)
Picking a standard prefix for your constant (and function) names makes
them much easier to discover when typing in a formula.
-Govert
On Jul 17, 9:40 pm, Farhan Ahmed <
far...@ycharts.com> wrote:
> Alright, figured it out - the function had to be decorated as
> 'IsMacroType=true'.
>
> Would love help with the AutoOpen() question though.
>
>
>
>
>
>
>
>
>
> On Tue, Jul 17, 2012 at 2:28 PM, Farhan Ahmed <
far...@ycharts.com> wrote:
> > Cool, thanks.
>
> > I must be doing something wrong since I can't get it to work. Here's my
> > code snippet which I've put at the top of a UDF (that's another question,
> > see later):
>
> > Excel.Application app =
> >> (Excel.Application)ExcelDnaUtil.Application;
> >> MessageBox.Show(app.ActiveWorkbook.Names.Count.ToString());
> >> app.ActiveWorkbook.Names.Add("FOO", 1234);
>
> > The message box correctly shows the count as 0, suggesting that I can
> > correctly get references to the Application and Workbook instances. The
> > last line throws an exception though (I am assuming because the call
> > returns from the UDF at this point, though the debugger doesn't break).
> > Anything obviously wrong that I am doing here?
>
> > Also, I want to put this code in the AutoOpen() so that the names are
> > defined for the user from the start. ActiveSheet is not yet defined at the
> > time the AutoOpen() is called. Is ActiveWorkbook guaranteed to exist by the
> > time AutoOpen() is called or is there a timing issue lurking here?
>
> > Thanks.
>
> > On Mon, Jul 16, 2012 at 4:41 PM, Patrick O'Beirne <
obeirne....@gmail.com>wrote:
>
> >> On 16/07/2012 20:13, Farhan Ahmed wrote:
>
> >>> Thanks Patrick and Naju.
>
> >>> Name Manager looks to be promising. Have you used it through the
> >>> ExcelDNA abstraction? I'm looking for a simple get/set example.
>
> >> ActiveWorkbook.Names.Add "Foobar",1243
>
> >> Will set (create or update) a name's value
>
> >> ActiveWorkbook.Names("Foobar")**.value
> >> gets the value. Also see RefersTo and RefersToRange for more flexibility.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Excel-DNA" group.
> >> To post to this group, send email to
exce...@googlegroups.com.
> >> To unsubscribe from this group, send email to exceldna+unsubscribe@**
> >>
googlegroups.com <
exceldna%2Bunsu...@googlegroups.com>.
> >> For more options, visit this group athttp://
groups.google.com/**
> >> group/exceldna?hl=en <
http://groups.google.com/group/exceldna?hl=en>.