Hello,

I have an app that use an excel component(ocxt.com). This component have a strange behavior, which I reproduced it with Interop.Excel.

Bellow is the functions with I open and close an excel file programatically:

private ApplicationClass app = null;
private Workbook book = null;
private Worksheet sheet = null;

...

private void openExcel(string fileName)
{
	if (app != null)
		closeExcel();
	app = new ApplicationClass();

	app.Visible = false;
	app.ScreenUpdating = false;
	app.DisplayAlerts = false;
	book = app.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
	sheet = (Worksheet)book.Sheets[1];
}

void closeExcel()
{
	if (sheet != null)
	{
		System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
		sheet = null;
	}

	if (book != null)
	{
		book.Close(false, Missing.Value, Missing.Value);
		System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
		book = null;
	}
	

	if (app != null)
	{
		app.Quit();
		System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
	}
	app = null;

	GC.Collect();
}

If I have 2 files: 1.xls and 2.xls and 1.xls I always open with .net code, and 2.xls double clicking it. There is 2 undesired (for me) behaviors:

1. If I open first by code file 1.xls, and then try to open 2.xls from Windows Explorer, 2.xls doesn't open. It only open an inactive Excel.
2. When I close by code 1.xls, it close 2.xls too.

What I do wrong?
Thank you.

Create a shortcut of 2.xsl file. This shortcut will opens an xsl file with new instance of Excel.

The best solution is

excelApp.IgnoreRemoteRequests = true;

on application startup, and reset it on close application.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.