Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to get a reference to the Excel application opened by your web application and still running. When you have this reference you could work with the Excel.Interop and save/close your workbook with the desired name</p> <pre><code>public bool SaveOpenExcel(string fileName) { int iSection = 0, iTries = 0; Microsoft.Office.Interop.Excel.Application oExcel; tryAgain: try { iSection = 1; // Attempting GetObject. oExcel = (Microsoft.Office.Interop.Excel.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); iSection = 0; // Resume normal error handling. oExcel.ActiveWorkbook.SaveAs(@"d:\temp\running.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); oExcel.Quit(); oExcel = null; return true; } catch (Exception err) { if (iSection == 1) { //GetObject may have failed because the //Shell function is asynchronous; enough time has not elapsed //for GetObject to find the running Office application. Wait //1/2 seconds and retry the GetObject. If you try 20 times //and GetObject still fails, assume some other reason //for GetObject failing and exit the procedure. iTries++; if (iTries &lt; 20) { System.Threading.Thread.Sleep(500); // Wait 1/2 seconds. goto tryAgain; //resume code at the GetObject line } else { Console.WriteLine("GetObject still failing. Process ended."); return false; } } else { //iSection = 0 so use normal error handling: Console.WriteLine(err.Message); return false; } } } </code></pre> <p>This code sample has been adapted from the Microsoft Support Article regarding a similar issue with WinWord. You could <a href="http://support.microsoft.com/kb/316125" rel="nofollow">read the article</a> to better understand the problems with this kind of approach.<br> I really suggest to change your Web Application to complete its task saving the file.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload