issue with closing excel object with C# : General Questions

issue with closing excel object with C#

Ask general questions here.

issue with closing excel object with C#

Postby vdeore » Tue Oct 04, 2011 8: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
vdeore
 
Posts: 11
Joined: Tue Oct 04, 2011 7:54 am

Re: issue with closing excel object with C#

Postby Todor » Tue Oct 04, 2011 9:12 am

Have you tried it like that?:

Code: Select all
using System.Diagnostics;

....


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

Todor
 
Posts: 66
Joined: Mon Jul 25, 2011 12:28 pm

Re: issue with closing excel object with C#

Postby vdeore » Wed Oct 05, 2011 2: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
vdeore
 
Posts: 11
Joined: Tue Oct 04, 2011 7:54 am

Re: issue with closing excel object with C#

Postby sdaly » Wed Oct 05, 2011 3: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();
}
}
}
User avatar
sdaly
 
Posts: 213
Joined: Mon May 10, 2010 12:04 pm
Location: Dundee, Scotland

Re: issue with closing excel object with C#

Postby vdeore » Wed Oct 05, 2011 4: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?
vdeore
 
Posts: 11
Joined: Tue Oct 04, 2011 7:54 am

Re: issue with closing excel object with C#

Postby sdaly » Wed Oct 05, 2011 4:49 pm

If you read it, you will see :?
User avatar
sdaly
 
Posts: 213
Joined: Mon May 10, 2010 12:04 pm
Location: Dundee, Scotland

Re: issue with closing excel object with C#

Postby Support Team » Wed Oct 05, 2011 6: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
User avatar
Support Team
Site Admin
 
Posts: 4845
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

Re: issue with closing excel object with C#

Postby vdeore » Fri Apr 13, 2012 3:52 pm

Hi,
Does Ranorex support identifying PDF object and reading the content within?
vdeore
 
Posts: 11
Joined: Tue Oct 04, 2011 7:54 am

Re: issue with closing excel object with C#

Postby Support Team » Mon Apr 16, 2012 5: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
User avatar
Support Team
Site Admin
 
Posts: 4845
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria


Return to General Questions

Who is online

Users browsing this forum: No registered users and 0 guests