This project has moved and is read-only. For the latest updates, please go here.

Cast PIA object to NetOffice object

Jan 13, 2013 at 12:57 AM
Edited Jan 13, 2013 at 12:58 AM

I have a VSTO Excel 2013 addin prioject using PIA.  There appears to be a bug in the PIA for Excel 2013.  The same method works fine in VBA, and I tested it in NetOffice and it works there too.  It is just a PIA problem.  I don't want to re-work my whole VSTO project and convert it all to NetOffice.  I would like to incorporate NetOffice for this one Excel method.

Is there a way to cast an existing PIA Excel object to a NetOffice object?  If I try myself to set the Excel NetOffice object equal to the existing PIA object, here is what I get:

Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to class type 'NetOffice.ExcelApi.Application'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

This would be a big help for me if it could be done.  No telling when or if MS will fix the bug and it is central to my app.

Thanks so much,

Craig

Jan 13, 2013 at 2:25 AM

yes of course, NetOffice is a wrapper and wraps the com proxies for you. the pia excel object is just a com proxy with some intellisense magic to give you the feel its  more than that. wrap the proxy as follows:

Excel.Application app = new Excel.Application(null, piaProxy);

// your code

app.Quit();       // terminate excel
app.Dispose(); // free the application in memory

Sebastian

btw: you can compare NetOffic objects with the "==" or (!=) operator to see its the same(workbook or worksheet for example)

Jan 13, 2013 at 4:25 AM
Edited Jan 13, 2013 at 4:33 AM

That totally solved my problem and turned Microsoft lemons into lemonade!  Now it doesn't matter if they don't fix the bug.

I guess the only thing to confirm is that it appears I shouldn't call dispose until my VSTO addin shuts down.  Here's the exception if I dispose of NetOffice object and later try to access my VSTO Globals.Thisaddin.Application:

{"COM object that has been separated from its underlying RCW cannot be used."}

Does it consume much memory or cause other problems to have this Netoffice application object instantiated in addition to the globals.thisaddin.application?  Do you see another way around it?

Or, is there any problem with setting NetOffice object to nothing instead of dispose?  Will it get cleaned up when VSTO shuts down?  That is, is the NetOffice creating a new COM or using the existing one from PIA which will get released when VSTO shuts down?  Am sorry about the question but this is all confusing to me.

Craig

Jan 13, 2013 at 3:06 PM

you can hold the instance in memory if course.
an example:

Excel.Application app = new Excel.Application(null, piaProxy);
foreach(var item in app.Workbooks)
{
  ; // nothing
}

you have com proxy tree in NetOffice now:

 - app
   - workbooks
       - enumerator
           - book1
           - book2
           - book3

you can hold the app instance but make sure to flush alle the child proxies.
do that with

app.DisposeChildInstance();

in ouer example, the call dipose the 5 child proxies and keep the application intance alive. you can still use the excel pia proxy in VSTO now.
the count of com proxies(managed from NetOffice) in your application is obseravble.

int NetOffice.Factory.ProxyCount; // just a static property
NetOffice.Factory.ProxyCountChanged // change event

have a look at the Tutorials in the release package for further info and example code snippets.

Sebastian

Jan 13, 2013 at 4:10 PM

This is genius.  What a wonderful product.  Thank you so much.

Craig