Hi,
I am facing issue with closing excel object in C#. I am using the following code.
###########################################
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(worksheet);
Marshal.FinalReleaseComObject(excelObj);
GC.Collect();
excelObj.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
##############################################
Please help me with an solution to this issue.
Thanks,
vdeore
issue with closing excel object with C#
Re: issue with closing excel object with C#
Have you tried it like that?:
Code: Select all
using System.Diagnostics;
....
Process[] close = Process.GetProcessesByName("EXCEL.EXE");
close[0].Close();
Re: issue with closing excel object with C#
I tried this solution and it didn't work.
Instead a workaround to close excel process is simply to kill the excel, but since that have the chances of corrupting the excel we can't use it.
code to kill excel
########################
ExcelProcess.Kill();
########################
Also tried the following thing, but this also doesn't work
#################################################
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(worksheet);
Marshal.FinalReleaseComObject(excelObj);
GC.Collect();
GC.WaitForPendingFinalizers();
#################################################
Also tried by setting the excel object to null, but noting works other than kill excel process.
Please suggest me for any more workarounds
Instead a workaround to close excel process is simply to kill the excel, but since that have the chances of corrupting the excel we can't use it.
code to kill excel
########################
ExcelProcess.Kill();
########################
Also tried the following thing, but this also doesn't work
#################################################
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(worksheet);
Marshal.FinalReleaseComObject(excelObj);
GC.Collect();
GC.WaitForPendingFinalizers();
#################################################
Also tried by setting the excel object to null, but noting works other than kill excel process.
Please suggest me for any more workarounds
Re: issue with closing excel object with C#
You could try closing the main window which will allow Excel time to close down properly, after 20 secs if it still hasn't closed then it will perform a hard kill on it... Try it!
public static void CheckAndKillProcess(string processName, bool gracefullKill)
{
Process[] processesToKill = Process.GetProcessesByName(processName);
foreach (Process process in processesToKill) {
if (gracefullKill) {
Report.Info("Gracefull kill " + process.ToString());
process.CloseMainWindow();
process.WaitForExit(20000);
if (!process.HasExited) {
Report.Info("Process never ended after 20 secs so killing");
process.Kill();
}
} else {
Report.Info("Killing " + process.ToString());
process.Kill();
}
}
}
Re: issue with closing excel object with C#
Thanks, its working. But i have a question here.
what i was using as mentioned earlier was ExcelProcess.Kill();
the method i am using is
public void KillExcel()
{
Process[] AllProcesses = Process.GetProcessesByName("Excel");
foreach ( Process ExcelProcess in AllProcesses) {
ExcelProcess.Kill();
Process[] AllProcesses1 = Process.GetProcessesByName("Excel");
if (AllProcesses1.Length < 1)
{
break;
}
}
}
So how is this different from the one that you had mentioned?
what i was using as mentioned earlier was ExcelProcess.Kill();
the method i am using is
public void KillExcel()
{
Process[] AllProcesses = Process.GetProcessesByName("Excel");
foreach ( Process ExcelProcess in AllProcesses) {
ExcelProcess.Kill();
Process[] AllProcesses1 = Process.GetProcessesByName("Excel");
if (AllProcesses1.Length < 1)
{
break;
}
}
}
So how is this different from the one that you had mentioned?
Re: issue with closing excel object with C#
If you read it, you will see 

- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: issue with closing excel object with C#
The Ranorex way of searching for all Excel forms and closing them would be something similar to this:
Regards,
Alex
Ranorex Team
var excelForms = Ranorex.Host.Local.Find<Ranorex.Form>( "/form[@processname='EXCEL']"); foreach (var ef in excelForms) ef.Close();Just my two cents...

Regards,
Alex
Ranorex Team
Re: issue with closing excel object with C#
Hi,
Does Ranorex support identifying PDF object and reading the content within?
Does Ranorex support identifying PDF object and reading the content within?
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: issue with closing excel object with C#
Hi,
Regards,
Peter
Ranorex Team
Ranorex supports reading PDF files, but this also depends on the reader if he provides accessibility to your elements. We test PDF files with Adobe Acrobat Reader.vdeore wrote:Does Ranorex support identifying PDF object and reading the content within?
Regards,
Peter
Ranorex Team