ESRI AddIn and Excel instance problem

825
1
06-04-2014 06:21 AM
DiegoPortillo
New Contributor III
Good morning. I have a programing question related to ArcGIS and Excel.

We are developing an AddIn that creates and opens a report in Excel. We want to open the report using, if one is already running, an existing running instance of Excel. If there is not one running, then start one. Currently we are getting mix results. The code that implements this logic is:

public Microsoft.Office.Interop.Excel.Application InitExcel(string macroName)
        {
            Microsoft.Office.Interop.Excel.Application exApp = null;
            Microsoft.Office.Interop.Excel.Workbook openWrkbk = null;
            Microsoft.Office.Interop.Excel.Workbook exWbk = null;

            try
            {
                //Determine if there is an open instance of Excel running. If so, we will
                //open the results in the same application. Otherwise, start a new
                //instance of Excel.
                try
                {
                    //First, kill any hung instances of Excel--ones that have no window.
                    Process[] processlist = Process.GetProcessesByName("Excel"); //Shows number of running Excel apps
                    foreach (Process theprocess in processlist) //foreach Excel app running
                    {
                        if (theprocess.MainWindowHandle.ToInt32() == 0)
                            try
                            {
                               theprocess.Kill();
                            }
                            catch { }
                    }
                    try
                    {
Here:                    exApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
                    }
                    catch (Exception err)
                    {
Tohere:                  MessageBox.Show("exception from marshal.getactiveobject " + err.ToString()); // ERROR EVERY TIME ON FIRST CALL
                    }
                    if (!exApp.Visible)
                    {
                        exApp = new Microsoft.Office.Interop.Excel.Application();
                    }
                }
                catch
                {
                    exApp = new Microsoft.Office.Interop.Excel.Application();
                }

The line labeled ???Here:??? is the line that checks to see if excel is already running.  It should return null or an Excel application object.  The first time it is called, it throws and exception every time.  To be specific, this exception [ATTACH=CONFIG]34333[/ATTACH]

The addin then goes on to start a new instance of Excel, and in subsequent calls to InitExcel, the call to marshal.getobject returns an Application Object that references this 2nd instance of excel.

I???ve written a test program that does this same exact thing, in the same way. It???s just a simple standalone desktop application. I click a button; it makes the call to marshal.getactiveobject.  I cannot make it throw an exception; it behaves as expected.  If excel is already running, it returns an object that references that instance of excel.  If excel is not running, it returns null.

This makes me think that somehow, ArcMap is turning off an rpc service, or otherwise ???blocking??? the addin code from marshaling somehow. Any ideas or thoughts would be greatly appreciated.

Thank you
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor
Diego,

I'm not a C# developer so I find all the curly brackets confusing as hell! Anyway if I have understood your logic you first kill off all existing processes? Then you call the GetActiveObject("Excel.Application") line and you get your COM exception error. Well reading the help on that it does say it returns a com Exception error if nothing exists, and that's not surprising as your first bit of the code killed off everything! So your com exception is your "Null reference", I guess you simply create an instance in the catch section?

Duncan
0 Kudos