Accessing Application.ThisCell from XLL UDF

Add-in Express™ Support Service
That's what is more important than anything else

Accessing Application.ThisCell from XLL UDF
 
Dan Evans




Posts: 27
Joined: 2014-06-18
I need to be able to return the last value of a UDF when certain contraints are not met. Within VBA I would have used the application.thisCell to access the current cell.

However this is returning a runtime error

I can reproduce the issue using this simple UDF


            public static string MyAddress()
            {
                try
                {
                    object hostApp = CurrentInstance.HostApplication;
                    Microsoft.Office.Interop.Excel.Application app =
                        hostApp as Microsoft.Office.Interop.Excel.Application;

                    Range myCell = (app.Application.ThisCell as Range);
                    return myCell.get_Address();
                }
                catch (Exception ex)
                {
                    return "Error : " + ex.Message;
                }
            }            }


Is this an excel com interop issue? is it possible to access the cell where the UDF is placed from an addinexpress UDF?

Thanks in advance for any help
Posted 07 Nov, 2014 04:19:21 Top
nwein




Posts: 577
Joined: 2011-03-28
If you want to get the address of the cell in an XLL UDF you can do
public static string MyAddress() 
{ 
    try 
    { 
        var callerRef = (ADXExcelRef)Module.CallWorksheetFunction(ADXExcelWorksheetFunction.Caller);
		string address = callerRef.ConvertToA1Style();
        return address; 
    } 
    catch (Exception ex) 
    { 
        return "Error : " + ex.Message; 
    } 
}
Posted 07 Nov, 2014 10:44:44 Top
Andrei Smolin


Add-in Express team


Posts: 18794
Joined: 2006-05-11
Thank you, Nir!

Dan,

Does Nir's suggestion help?


Andrei Smolin
Add-in Express Team Leader
Posted 10 Nov, 2014 03:53:16 Top
Dan Evans




Posts: 27
Joined: 2014-06-18
Awesome, thanks for you help.
I was not aware of the ADXExcelWorksheetFunction.

Regards
Dan
Posted 10 Nov, 2014 04:21:24 Top