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
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