0

I have an application which writes values to one of 4 cells in an Excel worksheet. The sheet is roughly 30x30 cells. Normally this works fine but if the user happens to click on the worksheet while my app is running then a COMException is thrown. Note that the cell that is clicked on does not have to be one of those that is being written to by the app. I tried doing 3 retries (see code below) but this did not help.

What might be causing this error and what can I do to handle it better?

public bool WriteString(string id, string value)
{
    for (int i = 0; i < RETRY; i++)
    {
        try
        {
            log.Debug(string.Format("XL write '{0}' to '{1}'", value, id));
            Range range = GetCell(id);
            range.Value2 = value;
            return true;
        }
        catch (System.Runtime.InteropServices.COMException x)
        {
            log.Error(x);
            Thread.Sleep(1);
        }
    }
    return false;
}

System.Runtime.InteropServices.COMException (0x800AC472): Exception from HRESULT: 0x800AC472
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Microsoft.Office.Interop.Excel.Range.set_Value2(Object value)
   at Pruef.Net.TgtManager.StdTarget.Excel.ExcelTarget.WriteString(String id, String value)
   at Pruef.Net.TgtManager.StdTarget.Excel.ExcelTarget.WriteDouble(String id, Double value)
   at Pruef.Net.Model.IOManaged.DoOutput(OutputAction action)
   at Pruef.Net.ViewModel.ExecuteViewModel.PerformAction(BaseAction action, Boolean ignoreStop)
   at Pruef.Net.ViewModel.ExecuteViewModel.PerformAction(BaseAction action, Boolean ignoreStop)
   at Pruef.Net.ViewModel.ExecuteViewModel.PerformActions(ObservableCollection`1 actions, Boolean ignoreStop)
   at Pruef.Net.ViewModel.ExecuteViewModel.Execute(UnitOfTest uot)

Just found this question Exception from HRESULT: 0x800A03EC Error. Perhaps 'sleep'ing a bit longer might help...

Community
  • 1
  • 1
paul
  • 12,710
  • 21
  • 76
  • 136

0 Answers0