writing to Excel from C#

Posted on 2007-08-08
Last Modified: 2008-01-09
I write to an Excel spreadsheet using the techniques shown below.  If the user is working in (I think saving) a different worksheet while my worksheet is filling, he will get this error:
Exception from HRESULT:0x800AC472

These are Windows XP workstations

What is causing this?

Excel.Application oApp;
Excel._Workbook oBook;
Excel.Workbooks oBooks;
Excel.Sheets oSheets;
Excel._Worksheet oSheet;
Excel.Range range;
long l = 0;

oApp = new Excel.Application();
oBooks = oApp.Workbooks;
oBook = oBooks.Add(Missing.Value);

      //let them tell us the spreadsheet name, etc.
      this.sfdMain.CheckFileExists = false;
      this.sfdMain.CheckPathExists = false;
      this.sfdMain.Title = "Select Excel File Path";
      this.sfdMain.Filter = "Excel Files (*.xls) | *.xls";
      sFile = sfdMain.FileName;
      if (sFile.Trim().Length == 0)
            throw new Exception("No File Name Chosen");
      if (File.Exists(sFile))
            this.sbp1.Text = "Deleting old file ...";
      //run several stored procedures here
      //read table using proprietary object
      //do the spreadsheet thing
      //set up workbook
      oSheets = oBook.Worksheets;
      oSheet = (Excel._Worksheet)oSheets.get_Item(1);
      oSheet.Name = "Master";
      range = oSheet.get_Range("a1",Missing.Value);
      range.get_Offset(0,1).set_Value(Missing.Value,"Price Report As Of : " + DateTime.Now.ToString());
      l = 1;
// other possible header lines here

      //blank line
      l += 1;
      //header  sample fields
      range.get_Offset(l,0).set_Value(Missing.Value,"Account Number");
      range.get_Offset(l,1).set_Value(Missing.Value,"Management Group");
      range.get_Offset(l,2).set_Value(Missing.Value,"Customer Type");
      range.get_Offset(l,63).set_Value(Missing.Value,"SC Item U/Comparison");
      //dates  sample formating
      range.get_Offset(l,3).EntireColumn.NumberFormat = "MM/DD/YYYY";
      range.get_Offset(l,23).EntireColumn.NumberFormat = "##,##0.00";
      //advance line
      l += 1;

// looping through a recordset

// when finished looping;
      //reset column widths
//save workbook


catch (Exception ex)
      MessageBox.Show(this, "Error Running Report | " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            //do nothing

Question by:g_johnson
    LVL 53

    Assisted Solution

    Do you mean that while your procedure is running (and filling the excel) the user switches to another workbook, sheet and gets the error ?
    In that case, I won't show the excel-sheet while filling.

    oApp = new Excel.Application();
    oApp.Visible = false;
    LVL 4

    Author Comment

    No, it's when they are working in any other workbook, I believe.  I will add the visible = false, though -- that just makes sense to me!
    LVL 3

    Accepted Solution

    LVL 4

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
    This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now