Is it possible to close an excel file when it is already open? I have written a code that can determine if a specific excel file is already open, however I cannot close the file once it has been determined to be open. I have tried the following method (see below) to close a workbook and excel application:
// The name of my workbook is "workbook", while the Excel application is named "excel."
workbook.Close(true);
excel.Quit();
Performing the latter code does not close the already open Excel window. It may also be of assistance to know the code I am using to determine if a file is open (it is provided below):
// The following checks to see if a file is open and returns truth1 as "true" if the file is open and "false" if the file is closed.
file = new FileInfo(file_name);
truth1 = IsFileinUse(file);
// ...
protected static bool IsFileinUse(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
return true;
}
finally
{
if (stream != null)
stream.Close();
}
return false;
}
Again, I cannot create a program in which I "kill Excel". I just need know how to close an already open Excel window if its path is the same as the one I am trying to read and write to.