title: Excel interop COM exception while running in background
body: <p>I'm trying to do apply some styles to cells in my workbook. And I want to do it in background thread so my GUI can stay responsive. This job should take few seconds, and if I click on some random cell in my document I'll get an exception. Here is my code:</p>
  
  <pre><code>public void ApplyStyles()
  {
      BackgroundWorker bw = new BackgroundWorker();
      bw.DoWork += DoWork;
      bw.RunWorkerAsync();
  
  }
  
  private void DoWork(object sender, DoWorkEventArgs e)
  {
      try
      {
          foreach (ICell xcell in cells)
          {
              Microsoft.Office.Interop.Excel.Range cell = cellUtility.GetCell(xcell);
              if (styles.ContainsKey(styleIds[xcell.Style]))
              {
                  Style s = styles[xcell.Style];
                  cell.Style = s;
              }
          }
      }
      catch (Exception ex)
      {
          if (Logger.IsErrorEnabled)
          {
              Logger.Error(ex.ToString());
          }
          messageBox.ShowErrorMessage(localizationMessages.ApplyingErrorText, localizationMessages.ApplyingErrorCaption);
      }
  }
  </code></pre>
  
  <p>When exception happens, this is the message I'm receiving;</p>
  
  <pre><code>System.Runtime.InteropServices.COMException (0x800AC472): Exception from HRESULT: 0x800AC472
     at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&amp; msgData)
     at Microsoft.Office.Interop.Excel.Range.set_Style(Object value)
     at ABZ.ReportFactory.OfficeAddin.Excel.BatchLinking.BackgroundStyleApplier.DoWork() in C:\ABZ\ABZ ReportFactory Office Addin\ABZ.ReportFactory.OfficeAddin.Excel\BatchLinking\BackgroundStyleApplier.cs:line 86
  </code></pre>
  
  <p>Is it possible to do this style applying operation in background thread? And how should I do it?</p>
  
  <p>Second problem I have is that while this style applying is running in background my cursor is constantly changing state from busy to regular until this operation is over. I would like for cursor to be normal. That user is totally unaware of this background operation.</p>
  
  <p>cheers,
  Vladimir</p>