0

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.

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by crispusx
0

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

0

The best solution is

excelApp.IgnoreRemoteRequests = true;

on application startup, and reset it on close application.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.