title: writing data from C# to Excel interrupted by opening Excel Window
body: <p>While my C# program writes data continuously to an Excel spreadsheet, if the end user clicks on the upper right menu and opens the
  <strong>Excel Options</strong> window, this causes following exception:</p>
  <blockquote>
  <p>System.Runtime.InteropServices.COMException with HRESULT: 0x800AC472</p>
  </blockquote>
  <p>This interrupts the data from being written to the spreadsheet.</p>
  <p>Ideally, the user should be allowed to do this without causing an exception.</p>
  <p>The only solution I found to this error code was to loop and wait until the exception went away:
  <a href="https://stackoverflow.com/questions/1952759/exception-from-hresult-0x800ac472">Exception from HRESULT: 0x800AC472</a>
  which effectively hangs the app, data is not written to Excel and the user is left in the dark about the problem.</p>
  <p>I thought about disabling the main menu of Excel while writing to it, but cannot find a reference on how to do this.</p>
  <p>My app supports <strong>Excel 2000</strong> to <strong>2013</strong>.</p>
  <h3>Here is how to reproduce the issue:</h3>
  <ul>
  <li><p>Using <strong>Visual Studio Express 2013</strong> for Windows Desktop, <strong>.NET 4.5.1</strong> on Windows 7 64-bit with <strong>Excel 2007</strong>, create a new Visual C# Console Application project.</p>
  </li>
  <li><p>Add reference to &quot;<strong>Microsoft ExceL 12.0 Object Library</strong>&quot; (for Excel) and to &quot;System.Windows.Forms&quot; (for messagebox).</p>
  <p>Here is the complete code:</p>
  <pre><code>  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading.Tasks;
    using System.Threading; // for sleep
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Reflection;
    using Microsoft.Win32;
    using Excel = Microsoft.Office.Interop.Excel; 
  
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i = 3; // there is a split pane at row two
                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
  
                try 
                { 
                    object misValue = System.Reflection.Missing.Value;
  
                    xlApp = new Excel.Application();
                    xlApp.Visible = false;
                    xlWorkBook = xlApp.Workbooks.Add(misValue);
  
                    xlApp.Visible = true;
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    // next 2 lines for split pane in Excel:
                    xlWorkSheet.Application.ActiveWindow.SplitRow = 2; 
                    xlWorkSheet.Application.ActiveWindow.FreezePanes = true;
                    xlWorkSheet.Cells[1, 1] = &quot;Now open the&quot;;
                    xlWorkSheet.Cells[2, 1] = &quot;Excel Options window&quot;;
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    System.Windows.Forms.MessageBox.Show(&quot;Microsoft Excel does not seem to be installed on this computer any longer (although there are still registry entries for it). Please save to a .tem file. (1)&quot;);
                      return;
                }
                catch (Exception)
                {
                    System.Windows.Forms.MessageBox.Show(&quot;Microsoft Excel does not seem to be installed on this computer any longer (although there are still registry entries for it). Please save to a .tem file. (2)&quot;);
                    return;
                }
  
                while(i &lt; 65000)
                {
                    i++;
  
                    try
                    {
                        xlWorkSheet.Cells[i, 1] = i.ToString();
                        Thread.Sleep(1000);
                    }
                    catch (System.Runtime.InteropServices.COMException)
                    {
                        System.Windows.Forms.MessageBox.Show(&quot;All right, what do I do here?&quot;);
                    }
                    catch (Exception) 
                    {
                        System.Windows.Forms.MessageBox.Show(&quot;Something else happened.&quot;);    
                    }
                }
  
                Console.ReadLine(); //Pause
            }
        }
    }
  
  </code></pre>
  </li>
  <li><p>Lanch the app, Excel appears and data is written to it. Open the Excel options dialog window from the menu and up pops the error:</p>
  <blockquote>
  <p>An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary <br>
  Additional information: Exception from HRESULT: 0x800AC472</p>
  </blockquote>
  </li>
  <li><p>Click on Continue and my message box <strong>&quot;All right, what do I do here?&quot;</strong> appears.</p>
  </li>
  </ul>
  <p>Please advise?</p>
  <p>Best regards,
  Bertrand</p>