issue with closing excel object with C#

Ask general questions here.
vdeore
Posts: 11
Joined: Tue Oct 04, 2011 6:54 am

issue with closing excel object with C#

Post by vdeore » Tue Oct 04, 2011 7:00 am

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

Todor
Posts: 66
Joined: Mon Jul 25, 2011 11:28 am

Re: issue with closing excel object with C#

Post by Todor » Tue Oct 04, 2011 8:12 am

Have you tried it like that?:

Code: Select all

using System.Diagnostics;

....


Process[] close = Process.GetProcessesByName("EXCEL.EXE");
close[0].Close();


vdeore
Posts: 11
Joined: Tue Oct 04, 2011 6:54 am

Re: issue with closing excel object with C#

Post by vdeore » Wed Oct 05, 2011 1:45 pm

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

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: issue with closing excel object with C#

Post by sdaly » Wed Oct 05, 2011 2:20 pm

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();
}
}
}

vdeore
Posts: 11
Joined: Tue Oct 04, 2011 6:54 am

Re: issue with closing excel object with C#

Post by vdeore » Wed Oct 05, 2011 3:24 pm

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?

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: issue with closing excel object with C#

Post by sdaly » Wed Oct 05, 2011 3:49 pm

If you read it, you will see :?

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: issue with closing excel object with C#

Post by Support Team » Wed Oct 05, 2011 5:03 pm

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
.
Image

vdeore
Posts: 11
Joined: Tue Oct 04, 2011 6:54 am

Re: issue with closing excel object with C#

Post by vdeore » Fri Apr 13, 2012 2:52 pm

Hi,
Does Ranorex support identifying PDF object and reading the content within?

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: issue with closing excel object with C#

Post by Support Team » Mon Apr 16, 2012 4:01 pm

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
.
Image