How to get reference to workbook from XLL UDF

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

How to get reference to workbook from XLL UDF
 
Jason Rodman




Posts: 40
Joined: 2016-11-01
I found the documentation no how to use a worksheet function to get the caller for a UDF, but all it gives me is the SheetId. How can I use that to get a reference to the actual worksheet and ultimately its parent workbook?

The reason I am doing this is I need to be able to read the DocumentProperties of the calling sheet's workbook within my UDF.
Posted 01 Nov, 2016 17:01:24 Top
nwein




Posts: 577
Joined: 2011-03-28
This should get you what you want:
var callerRef = Module.CallWorksheetFunction(ADXExcelWorksheetFunction.Caller) as ADXExcelRef;					
string caller = callerRef.ConvertToA1Style();
Posted 01 Nov, 2016 17:12:15 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Jason,

As to using the Excel object model in your UDF, please check http://support.microsoft.com/kb/301443.

Thank you, Nir!


Andrei Smolin
Add-in Express Team Leader
Posted 02 Nov, 2016 03:09:45 Top
Jason Rodman




Posts: 40
Joined: 2016-11-01
So from what you are saying there is no safe way to get an instance of the Workbook object to read its document properties from an XLL UDF without running the risk of crashing excel? I don't need to modify anything, just read its properties to make decisions in the UDF.
Posted 02 Nov, 2016 08:21:09 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
A safe way is to get such info from a COM add-in. The COM add-in should retrieve this info from the object model at startup.


Andrei Smolin
Add-in Express Team Leader
Posted 02 Nov, 2016 09:09:01 Top
Jason Rodman




Posts: 40
Joined: 2016-11-01
Is it safe to grab this info during the startup of an XLL addin too? Maybe when a new workbook is opened?
Posted 02 Nov, 2016 12:55:55 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Jason,

It looks promising but I don't know if it is actually safe or not.


Andrei Smolin
Add-in Express Team Leader
Posted 03 Nov, 2016 04:43:36 Top