Quantcast
Channel: Forums - ArcObjects SDKs
Viewing all articles
Browse latest Browse all 1374

ESRI AddIn and Excel instance problem

$
0
0
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 Attachment 34333

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
Attached Thumbnails
Click image for larger version

Name:	ExcelError.jpg‎
Views:	N/A
Size:	35.5 KB
ID:	34333  

Viewing all articles
Browse latest Browse all 1374

Trending Articles