The following code tries to attach to an excel session that has the C:\test file opened. If such a session doesn’t exist, this either returns null or throws an exception.
var myExcelWorkbook = Marshal.BindToMoniker(@"C:\test") as Excel.Workbook;
This works very well when the file has no extension. However if I am trying the exact same code with a file that has an extension (for instance C:\test.xlsb), the call tries to open that file with excel before returning. If an instance is already running, it will try to open the file in that instance (as if someone double-clicked on that file in the windows explorer).
var myExcelWorkbook = Marshal.BindToMoniker(@"C:\test.xlsb") as Excel.Workbook;
This definitely has to do with the extension (how would BindToMoniker know how to open Excel otherwise?), but is very annoying, since I never want to attach to an existing excel session that doesn’t have that file already opened.
Is there a way to disable that “feature”? I would like that the BindToMoniker method never tries to open a file and let me decide what to do in the various cases.
The issue is most likely with how Excel documents are named in “running objects table” rather than presence of extension.
Read on IRunningObjectTable… There is IRunningObjectTable.EnumRunning which may be useful.