Hi Nemo,
You need to take some care even from the VBA / Automation side with
the information that actually gets passed in from Excel's function
call. Say you have a function Function MyFunction(r as Range). When
Excel calls your function, you might receive a range object. But there
is no information visible from inside your function to know whether
the function is called with the range using the address or using a
name the refers to the range. If the name "MyName" refers to =Sheet1!$A
$1:$B$2 then =MyFunction(Sheet1!$A$1:$B$2) and =Myfunction(MyName)
will get exactly the same Range object. The
Range.Name.Name property
you use gives the alphabetically first name that has the range as its
value. If you have "AnotherName" pointing to the same range and try
=Myfunction(MyName) you'll find that
r.Name.Name returns
"AnotherName". The closest you can get to knowing how you were called
is to examine the formula of the caller. So it seems you are climbing
outside the Excel model a bit.
Having said all this, you can get the same behaviour in ExcelDna. You
can get a range into the ExcelDna function by tagging the object
argument with [ExcelArgument(AllowReference=true)]. You should then
receive an ExcelDna.Integration.ExcelReference object when your
function is called with a reference or a name referring to a reference
(you'll have to deal with cases where the function is called with a
value or name that refers to a value). With the ExcelReference object
you can get the first name for the referenced range using a call to
Excel(xlfGetDef, ... myRangeAddress ...). The address must be R1C1
style text address for the range - some xlf... function gives this
when passed in an ExcelReference. If you want to list all the names
defined in the book, you can call Excel(xlfNames, ...) to get back an
array of the names, and look for your range in there.
Anyway - it looks like you can get the same information in ExcelDna
that you get in VBA or Automation, though you have to be a bit careful
about what you are actually getting (and how you are using it).
If you need a more worked out example, let us know where you get
stuck.
Regards,
Govert