locked
hi RRS feed

  • Question

  • i am creating a excel file using the Excel object library(automation) in windows application using c#.net.while saving the excel file i have chosen the Save as option.
    1)workbook.saveas(....)
    The file has been saved successfully for the first time,,
    but from the next time onwards it is creating runtime error saying that exception from hresult:0x800A03EC
    Any help would be appreciated..



    Tuesday, August 5, 2008 11:34 AM

Answers

  • I've got nothing solid on this but perhaps you can take a look here and see if any of this matches your issue...

    /Calle
    - Still confused, but on a higher level -
    • Marked as answer by jack 321 Friday, August 8, 2008 2:53 AM
    Tuesday, August 5, 2008 12:11 PM
  • Hey... Arulmozhi.
    Finally figured this out.
    First off all, there is no mistake in your code.

    If you go to excell, create the simplest macro you can, then edit the macro:
    dim Macro1
      Application.ActiveWorkBook.SaveAs("C:\myfolder\myfile.doc")
    end dim

    You can run the macro once, second time it shows the "overwrite?" screen.  Pressing no means error too.  And this is with VBScript from INSIDE Microsoft Excell... :-S

    So basically, the window gives you the choice between overwrite or error.  Wow you might aswell have no choice at all...

    But wait! ;-) That's possible!  Just before you call SaveAs, write this (on oXL not oWB!)

    oXL.DisplayAlerts =
    false;  


    Result: the SaveAs() method works, and overwrites without asking, no more getting thrown out of your application.

    If you insist on asking before overwriting, you could use the System.IO.File.Exists method, in combination with a C# filesavedialog.

    Let us know how things went ;-)


    The improbable we do, the impossible just takes a little longer. - Steven Parker
    • Proposed as answer by Anuja Annie Ninan Wednesday, August 6, 2008 5:00 PM
    • Marked as answer by jack 321 Friday, August 8, 2008 2:52 AM
    Wednesday, August 6, 2008 1:15 PM

All replies

  • I've got nothing solid on this but perhaps you can take a look here and see if any of this matches your issue...

    /Calle
    - Still confused, but on a higher level -
    • Marked as answer by jack 321 Friday, August 8, 2008 2:53 AM
    Tuesday, August 5, 2008 12:11 PM
  • it would be better if you can post your piece of code in order to get any opinion. 
    Pradeep Sethi
    Tuesday, August 5, 2008 2:02 PM
  •  

    Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();

    Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));

    string path = @"c:\xmltest\data.xls";

    oXL.Visible = true;

    Excel.Sheets Totalsheets = oWB.Sheets;

    Excel._Worksheet oSHEET1 =(Excel._Worksheet) Totalsheets.get_Item("Sheet1");

    oSHEET1.Activate();

    oSHEET1.Cells[1, 1] = "Forms";

    oSHEET1.Cells[1, 2] = "Complexity";

    oSHEET1.get_Range("A1", "B1").Font.Bold = true;

    oSHEET1.get_Range("A1", "B1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;

    oSHEET1.Name = "Forms Data";

    for (int i = 0, m = 2; i < dtformexportcomplexity.Rows.Count; i++, m++)

    {

    oSHEET1.Cells[m, 1] = dtformexportcomplexity.Rows[i][0].ToString();

    oSHEET1.Cells[m, 2] = dtformexportcomplexity.Rows[i][1].ToString();

    }

    oSHEET1.get_Range("A1", "B7").EntireColumn.AutoFit();

    try

    {

    oWB.SaveAs(path,Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, false,false,Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

    // oWB.Close(false, Missing.Value, Missing.Value);

    }

    catch (Exception theException)

    {

    String errorMessage;

    errorMessage = "Error: ";

    errorMessage = String.Concat(errorMessage, theException.Message);

    errorMessage = String.Concat(errorMessage, " Line: ");

    errorMessage = String.Concat(errorMessage, theException.Source);

    MessageBox.Show(errorMessage);

    }

    finally

    {

    // oWB.Close(false, Missing.Value, Missing.Value);

    oXL.Quit();

    // MessageBox.Show("file saved at:" + path);

    }



    this is the code i have used to generate an excel .

    first time it is running fine.(it is creating the file first time in the specified location)

    second time it is giving a dialog box saying that do u want to overwrite the existing file?
    if we click yes it is overwriting the file
    if click no then it is opening new excel book
    at that time an exception is occured saying that exception from hresult:0x800a03ec


    how to solve this problem.


    give me an solution / approach to this problem
    thanks

    Wednesday, August 6, 2008 5:20 AM
  •  Use Save() instead of SaveAs() if you don't need to change the filename.
    Wednesday, August 6, 2008 7:58 AM
  •  try this in combination with Code_Breaker's answer.

    if (System.IO.File.Exists(@"C:\myfolder\myfile.doc"))

    { } //save

    else

    { } //saveAs


    The improbable we do, the impossible just takes a little longer. - Steven Parker
    Wednesday, August 6, 2008 8:04 AM
  • thanks for the solution. but

     Still i am facing the run time error problem.

    Can u provide me any material to do the excel automation using c#

    other wise please give me the idea about how to use workbook.saveas() method.

    in msdn they mentioned that first time while saving the excel sheet we have to use saveas() method.
    but next run either we hav to overwrite or we have to create a new one. creating a new one is easy.
    but how to overwrith the file. before saving it is iving a message box that do you want to overwrite the existing file?
    if click yes overwrite has done smoothly. if click no then runtime exception occurs.


    Wednesday, August 6, 2008 11:59 AM
  • Hey... Arulmozhi.
    Finally figured this out.
    First off all, there is no mistake in your code.

    If you go to excell, create the simplest macro you can, then edit the macro:
    dim Macro1
      Application.ActiveWorkBook.SaveAs("C:\myfolder\myfile.doc")
    end dim

    You can run the macro once, second time it shows the "overwrite?" screen.  Pressing no means error too.  And this is with VBScript from INSIDE Microsoft Excell... :-S

    So basically, the window gives you the choice between overwrite or error.  Wow you might aswell have no choice at all...

    But wait! ;-) That's possible!  Just before you call SaveAs, write this (on oXL not oWB!)

    oXL.DisplayAlerts =
    false;  


    Result: the SaveAs() method works, and overwrites without asking, no more getting thrown out of your application.

    If you insist on asking before overwriting, you could use the System.IO.File.Exists method, in combination with a C# filesavedialog.

    Let us know how things went ;-)


    The improbable we do, the impossible just takes a little longer. - Steven Parker
    • Proposed as answer by Anuja Annie Ninan Wednesday, August 6, 2008 5:00 PM
    • Marked as answer by jack 321 Friday, August 8, 2008 2:52 AM
    Wednesday, August 6, 2008 1:15 PM