exception 0x800A03EC in excel

J

Joao Mossmann

Hello

I need to save excel file, but the method saveas result in exception
HRESULT: 0x800A03EC
In System.Exception {System.Runtime.InteropServices.COMException}

I use this C# code:

/* ----------------- BEGIN CODE ----------------- */
using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Diagnostics;


namespace console

{

class Program

{
static Excel.Application xlApp;
static Excel.Workbook xlBook;
static Excel.Worksheet xlSheet1, xlSheet2, xlSheet3;
static Excel.AppEvents_WorkbookBeforeCloseEventHandler
EventDel_BeforeBookClose;
static Excel.DocEvents_ChangeEventHandler EventDel_CellsChange;
//InitializeComponent();
static void Main(string[] args)
{
try

{

//Start Excel, and then create a new workbook.
xlApp = new Excel.Application();
xlBook = xlApp.Workbooks.Add(Missing.Value);
xlBook.Windows.get_Item(1).Caption = "XL Event Test";
xlSheet1 = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);
xlSheet2 = (Excel.Worksheet)xlBook.Worksheets.get_Item(2);
xlSheet3 = (Excel.Worksheet)xlBook.Worksheets.get_Item(3);
xlApp.Visible = false;
xlApp.UserControl = false;
xlSheet1.Activate();
xlSheet1.Cells[1, 2] = "tESTE";
xlApp.ActiveCell.FormulaR1C1 = "10";
xlApp.ActiveWorkbook.SaveAs("c:\teste.xls", "xls", "", "",
null, null, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
null, null, null, null, null);

}

catch (Exception ex)

{

throw ex;

}

}

private void CellsChange(Excel.Range Target)

{

//This is called when any cell on a worksheet is changed.
Debug.WriteLine("Delegate: You Changed Cells " +
Target.get_Address(Missing.Value, Missing.Value,
Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value) +
" on " + Target.Worksheet.Name);
}


private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel)
{
//This is called when you choose to close the workbook in Excel.
//The event handlers are removed, and then the workbook is closed
//without saving the changes.
Wb.Saved = true;
Debug.WriteLine("Delegate: Closing the workbook and removing
event handlers.");
xlSheet1.Change -= EventDel_CellsChange;
xlSheet2.Change -= EventDel_CellsChange;
xlSheet3.Change -= EventDel_CellsChange;
xlApp.WorkbookBeforeClose -= EventDel_BeforeBookClose;
}

}
}
/* ----------------- END CODE ----------------- */
Thanks in advance
 
R

Ronald Dodge

The biggest issue I have with the SaveAs method in Excel, and there is no
way to turn it off is when overwriting an Excel file with the same file
name, the confirmation message box of verifying that you want to replace the
file in the locaion with the file currently open, which then stops the code
at that point. Even if you set the "DisplayAlerts" property to "False"
boolean value (or 0), it still happens with that particular situation. This
is why I do not use the "SaveAs" method on any workbook.

How to get around this issue?

Instead of using the "SaveAs" method, use the "SaveCopyAs" method as it does
the trick and avoids the confirmation message entirely.

There is one other distinction between the "SaveAs" method versus the
"SaveCopyAs" method. With the "SaveAs" method, any formulas of any open
workbook dependent on the workbook being saved via the "SaveAs" method, they
will adjust to the new location. Using the "SaveCopyAs" method, none of the
formulas will be adjusted, even if they are dependent on the workbook being
saved via the "SaveCopyAs" method. The main reason why this is the case,
the "SaveCopyAs" method is originally meant to be used as a backup method,
so if something happens to the primary file, you have a backup way of
retrieving the file without hurting anything. To retrieve the file from the
backup location, just use windows explorer to copy it from it's backup
location and paste it to the original location.

--

Sincerely,

Ronald R. Dodge, Jr.
Master MOUS 2000

Joao Mossmann said:
Hello

I need to save excel file, but the method saveas result in exception
HRESULT: 0x800A03EC
In System.Exception {System.Runtime.InteropServices.COMException}

I use this C# code:

/* ----------------- BEGIN CODE ----------------- */
using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Diagnostics;


namespace console

{

class Program

{
static Excel.Application xlApp;
static Excel.Workbook xlBook;
static Excel.Worksheet xlSheet1, xlSheet2, xlSheet3;
static Excel.AppEvents_WorkbookBeforeCloseEventHandler
EventDel_BeforeBookClose;
static Excel.DocEvents_ChangeEventHandler EventDel_CellsChange;
//InitializeComponent();
static void Main(string[] args)
{
try

{

//Start Excel, and then create a new workbook.
xlApp = new Excel.Application();
xlBook = xlApp.Workbooks.Add(Missing.Value);
xlBook.Windows.get_Item(1).Caption = "XL Event Test";
xlSheet1 = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);
xlSheet2 = (Excel.Worksheet)xlBook.Worksheets.get_Item(2);
xlSheet3 = (Excel.Worksheet)xlBook.Worksheets.get_Item(3);
xlApp.Visible = false;
xlApp.UserControl = false;
xlSheet1.Activate();
xlSheet1.Cells[1, 2] = "tESTE";
xlApp.ActiveCell.FormulaR1C1 = "10";
xlApp.ActiveWorkbook.SaveAs("c:\teste.xls", "xls", "", "",
null, null, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
null, null, null, null, null);

}

catch (Exception ex)

{

throw ex;

}

}

private void CellsChange(Excel.Range Target)

{

//This is called when any cell on a worksheet is changed.
Debug.WriteLine("Delegate: You Changed Cells " +
Target.get_Address(Missing.Value, Missing.Value,
Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value) +
" on " + Target.Worksheet.Name);
}


private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel)
{
//This is called when you choose to close the workbook in
Excel.
//The event handlers are removed, and then the workbook is
closed
//without saving the changes.
Wb.Saved = true;
Debug.WriteLine("Delegate: Closing the workbook and removing
event handlers.");
xlSheet1.Change -= EventDel_CellsChange;
xlSheet2.Change -= EventDel_CellsChange;
xlSheet3.Change -= EventDel_CellsChange;
xlApp.WorkbookBeforeClose -= EventDel_BeforeBookClose;
}

}
}
/* ----------------- END CODE ----------------- */
Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top