-2

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.

Marco Soto
  • 61
  • 1
  • 11
  • Do you already tried this one? http://stackoverflow.com/questions/22971269/closing-an-open-excel-workbook-in-c-sharp – Philipp Müller Feb 27 '17 at 13:14
  • The .close() command does not close the already open file; which is what is what is mainly focused on in all the solution I find on this subject (including the one you just recommended). – Marco Soto Feb 27 '17 at 13:27
  • Possible duplicate of [Closing an Excel Workbook](http://stackoverflow.com/questions/17440138/closing-an-excel-workbook) – Philipp Müller Feb 27 '17 at 14:17
  • 1
    It is not the same, as I cannot "kill Excel," which is what the leading solution to your link recommends. Also using .close() or .quit() does not remove the already open Excel file from the screen. – Marco Soto Feb 27 '17 at 14:29

2 Answers2

0

Please have a look on below sample code :-

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application exl = new Excel.Application();

# open a file
Excel.Workbook wbook = exl.Workbooks.Open("some_file.xlsx");

# To close the file
wbook.Close();

exl.Quit();

Edit 1:-

You can refer below link if above solution not works for you :-

Closing an Excel Workbook

Community
  • 1
  • 1
Anand Systematix
  • 632
  • 5
  • 15
  • The .Close() and .Quit() command **are not closing the already open Excel file.** I want to close the already open file. – Marco Soto Feb 27 '17 at 13:35
0

You can use the Windows API, to close a certain Window by it's title.

This will invoke a Window-Close, so it will prompt the user if he really wants to close:

using System.Runtime.InteropServices;

...

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

const UInt32 WM_CLOSE = 0x0010;

And then

IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "MyFile.xlsx - Excel");
if (windowPtr == IntPtr.Zero)
{
     MessageBox.Show("Document not open");
     return;
}

SendMessage(windowPtr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

If you need to force a close, even if the file has been modified, you only can kill the related excel-process. But keep in mind that this will forcefully kill other excel-windows as well, if they are running within the same excel-process-instance.

  using System.Diagnostics;
  ...
  Process[] processes = Process.GetProcesses();

  foreach (Process p in processes)
  {
      if (p.MainWindowTitle == "MyFile.xlsx - Excel")
      {
          p.Kill();
          break;
      }
  }
dognose
  • 20,360
  • 9
  • 61
  • 107
  • Is there way to close an Excel window without prompting the user, and without killing Excel. Thank you for your assistance. – Marco Soto Feb 27 '17 at 13:53