views:

241

answers:

1

Does anyone know if a site (even non-Microsoft), that has details on COMExceptions/HRESULTS.

When I attempted to Save my Excel Workbook after using the Copy() function, I received this error:

ERROR - Unable to SaveWorkbook()
System.Runtime.InteropServices.COMException (0x800A03EC): Document not saved.

P.S. I'm doing this in a tight loop for 10K+ files, but reading the files worked out fine, but saving them is proving to be not fun.

Also, if anyone has had the issue of Excel hemorrhaging memory when you use the Copy() function let me know.

Thanks!

P.S.S If anyone needs further clarification please let me know

Edit 1

Here's what's going on. I've been asked to update some XLS files (10K+ in fact), by copying the Active Sheet to a new Sheet (same Workbook) and update the original Sheet by performing some conversion. The problem comes when I save the Workbook.

Here is some excerpts from my application:

///.... UpdateSpreadsheet() Routine
Microsoft.Office.Interop.Excel.Workbook wb = null;

try
{
    wb = ef.GetWorkbook(fileName, false, true);
}
catch (Exception ex)
{
    logger.Error(ex.ToString());
}

Microsoft.Office.Interop.Excel.Worksheet ws = null;

try
{
    ws = wb.Sheets[Foo.WorksheetName] as Microsoft.Office.Interop.Excel.Worksheet;
}
catch (Exception ex)
{
    logger.Error(ex.ToString());
}

bool result = false;

if (ws != null)
    result = ef.CopyWorksheet(ws, Foo.WorksheetName);

if (result)
{
   //... update the sheet as appropriate
}

try
{
    ef.SaveWorkbook(wb, fileName);  //eventually this line crashes, it's random, but so far after the 500th file, and I get that COM Exception.
    //.. update Foo object to reflect copied worksheet
}
catch (Exception ex)
{
    //something happened, we can't save, so close and destroy the workbook
    logger.Error("Unable to SaveWorkbook()", ex); 
}
finally
{
    ef.DestoryWorkbook(wb, fileName);
    ef.DestroyWorksheet(ws);
}

//// CopyWorksheet() Method
public bool CopyWorksheet(Worksheet ws, String sourceSheet)
{
    try
    {
     try
     {
      Worksheet sheet = GetWorksheet(sourceSheet + " (2)");

                    //I don't think the below is necessary, but I'm paranoid about memory leaks
      ExcelTools.OfficeUtil.ReleaseRCM(sheet);
      sheet = null;

      return false;
     }
     catch (Exception)
     {
      ws.Copy(Missing.Value, ws); //this line never errors out
     }

     return true;
    }
    catch (Exception)
    {
     return false;
    }
    finally
    {
     ws.Activate();
    }
}

/// SaveWorkbook()
public void SaveWorkbook(Workbook wb)
{
    if (wb != null)
    {
     wb.Save();
    }
}
A: 

If you break down the HRESULT value, the facility code is FACILITY_CONTROL, and the error code is 1004. From some online searching, it appears that this code is generated for a variety of different errors, some of which seem related to copying worksheets programmatically (see for example this KB article).

It might help to post more details about what you're doing, or search online for this HRESULT and see what problems others may have had which look similar to what you're doing.

Charlie