Page 1 of 1

issue with closing excel object with C#

Posted: Tue Oct 04, 2011 7:00 am
by vdeore
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

Re: issue with closing excel object with C#

Posted: Tue Oct 04, 2011 8:12 am
by Todor
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#

Posted: Wed Oct 05, 2011 1:45 pm
by vdeore
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

Re: issue with closing excel object with C#

Posted: Wed Oct 05, 2011 2:20 pm
by sdaly
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#

Posted: Wed Oct 05, 2011 3:24 pm
by vdeore
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?

Re: issue with closing excel object with C#

Posted: Wed Oct 05, 2011 3:49 pm
by sdaly
If you read it, you will see :?

Re: issue with closing excel object with C#

Posted: Wed Oct 05, 2011 5:03 pm
by Support Team
The Ranorex way of searching for all Excel forms and closing them would be something similar to this:
var excelForms = Ranorex.Host.Local.Find<Ranorex.Form>(
    "/form[@processname='EXCEL']");
foreach (var ef in excelForms)
    ef.Close();
Just my two cents... :D

Regards,
Alex
Ranorex Team

Re: issue with closing excel object with C#

Posted: Fri Apr 13, 2012 2:52 pm
by vdeore
Hi,
Does Ranorex support identifying PDF object and reading the content within?

Re: issue with closing excel object with C#

Posted: Mon Apr 16, 2012 4:01 pm
by Support Team
Hi,
vdeore wrote:Does Ranorex support identifying PDF object and reading the content within?
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.

Regards,
Peter
Ranorex Team