Hi Naju,
Thanks for the reply. The purpose of the workbook ID is to match open workbook(s) to cached data models. The user may have multiple workbooks open simultaneously linked to different versions of the cached data model. When the UDF is evaluated, I need to pass this ID/key associated with the calling workbook to my out of proc service to know which cached data model to use and retrieve data from based on the version of the data model that was linked in my Excel workbook.
Ideally, I'd like this workbook ID to live as long as the workbook is open. It does not need to carry over between workbook close and workbook open--I can reestablish the connection at workbook open. I think I found a solution that will work for my needs.. let me know what you think. Basically, I need to get the calling reference to get the calling sheet name. Once I have the calling sheet name, I can get the calling workbook using XlCall.Excel(XlCall.xlfGetDocument, 88, sheetName). workbookName2 won't work in this case because that only returns the active worksheet name. This won't work if the user does a global recalculation (i.e. CTRL+ALT+F9). Once I know the calling workbook name, find that name in my static dictionary to get my workbook ID I created on workbook open. Since there can only be one workbook open at the same time with the same name, there won't be a chance of a collision.
// get calling excel reference
if (XlCall.Excel(XlCall.xlfCaller) is ExcelReference reference)
{
// get calling sheet name
var sheetName = XlCall.Excel(XlCall.xlSheetNm, reference) as string;
// get calling workbook name (will work because this returns the calling workbook based on the calling sheet)
var workbookName1 = XlCall.Excel(XlCall.xlfGetDocument, 88, sheetName) as string;
// get calling workbook name (won't work because this returns the active workbook as opposed to the calling workbook)
var workbookName2 = XlCall.Excel(XlCall.xlfGetWorkbook, 16) as string;
System.Diagnostics.Debug.WriteLine($"workbookName1: {workbookName1} / workbookName2: {workbookName2}");
// todo: lookup workbook name in static dictionary to retrieve workbook id created at workbook open
}
Thanks,
Will