Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I have a windows based application that opens excel,loads a file, makes changes as needed, saves it and quits.

I thought I had this covered with:

public void connectionClose()
{
xlApp.Save();
xlApp.Quit();

releaseObject(xlApp);
releaseObject(xlworkBook);
}


however after trying to open the file I had been altering to make sure it was doing as expected, I got an error saying can only see read only as its in use. Opened up task manager and there was 10-15 excel.exe running in the processes. Now I thought my solution would have closed them but apparently not. Is there away to ensure that when I save and quit it does actually get rid of excel, I don't want it taking up memory and also I want to be able to check what I'v done!

Cheers
Posted

If you want to quit the application after saving excel file. Then try this:
this.Close();

If you want to release any resources (managed or unmanaged) that were used by your program, then use the dispose function.
<br />
this.dispose();
 
Share this answer
 
Comments
DanHodgson88 26-Jul-11 10:23am    
this.Close(); won't close the excel app, it closes the actually application. But this.dispose() may work will have a try!
Hi,

I recently had this problem. I found a solution from fitting together a lot of other forum posts which I can't remember off the top of my head to credit.

My final stitched together solution will work, and is as follows:

using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.IO;
namespace ExcelExample
{
	class CreateExcelDoc
	{	
		Microsoft.Office.Interop.Excel.Application oXL;
		_Workbook oWB;
		_Worksheet oSheet;
		Range oRng;
		
		[DllImport("user32.dll")]
		private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
		Hashtable myHashtable = new Hashtable();
		public CreateExcelDoc()
		{
			//Start Excel and get Application object.
			oXL = new Microsoft.Office.Interop.Excel.Application();	
			//Get a new workbook.
			oWB = (_Workbook)(oXL.Workbooks.Add( Type.Missing ));
			oSheet = (_Worksheet)oWB.ActiveSheet;
		}
		
		public void SaveDocument()
		{
			try
			{
			oWB.SaveAs(@"Filename", XlFileFormat.xlWorkbookNormal,
			Type.Missing, Type.Missing,
			false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
			Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
			oWB.Close(null, null, null);
			oXL.Quit(); 			
			
			KillApplicationProcess(oXL);
			}
			catch( Exception theException ) 
			{
			    String errorMessage;
			    errorMessage = "Error: ";
			    errorMessage = String.Concat( errorMessage, theException.Message );
			    errorMessage = String.Concat( errorMessage, " Line: " );
			    errorMessage = String.Concat( errorMessage, theException.Source );
			}
		}
		public static void KillApplicationProcess(Microsoft.Office.Interop.Excel.Application oXL)
		{
			int hWnd = oXL.Application.Hwnd;
			uint processID;
			GetWindowThreadProcessId((IntPtr)hWnd, out processID);			
			Process.GetProcessById((int)processID).Kill();
		}
		
		public void AddData(int row, int col, string data)
		{
			oSheet.Cells[row, col] = data;		
		}
	}
}


Hope that helps :)
 
Share this answer
 
You could always use Process.Close or failing that Process.Kill. The problem would be that you'd have to make sure you are closing an Excel process that you opened and not one that a user just happen to have open when they started your application (or have opened separately since).
However, the fact that you have 10-15 excel processes running suggests the problem is somewhere else. Perhaps you are opening the file multiple times without realizing it? I'd check your code very carefully, especially where you are creating xlApp and xlworkBook in the first place, to make sure you aren't calling it multiple times.

Another thought, I haven't used Excel this way, but I did play around with PowerPoint, is there a Close method on the workbook? You might be missing an xlworkBook.Close prior to your xlApp.Quit.
 
Share this answer
 
v2
Comments
DanHodgson88 26-Jul-11 10:04am    
Well the multiple openings, I believe, has come from the testing. I hadn't realised that the prog wasnt closing excel properly so ran my app a few times and ended up with loads of excel's open.

I have checked the code and there isn't any multiples in the code. Lol its very frustrating!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900