469,271 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Can use C# to create and save excel file. But can't reopen the file as it crashes

Hi all,

Hope this is the right group.

I am writing a program in c# to open create an excel document, enter
some data, save it and then email it as an attachment. I have
successfully created an excel document which the user can see (at this
stage of development) and passed some data to it. I then used the
savas method to save the file. Again this seems to work as the file is
created. However once I close the excel file and try and reopen it
from the automatically saved version I get the following error
message, which results in it being impossible to open the excel doc.
This happens not just on my machine, but any computer that tries to
open these files.

Error message title bar reads as follows

Microsoft Excel DebugMy Sheet.xls: Excel.EXE Application Error

The body of the error message reads as follows

The instruction at "0x300045619" referenced memory at "0x00050018".
The memory could not be "read".
Click on OK to terminate the program
Click on Cancel to debug the program

I have research similar errors on Microsoft's site and have search
newsgroups. Although lots of people seem to get similar errors, they
seem to be caused by different situations and are not applicable to
what I am doing

I am using C# code and Excel 2000

I hope somebody has an answer

Here is my code
//create excel file and data
Excel.Application ExcelApp;
Excel._Workbook ExcelWorkbook;
Excel._Worksheet ExcelWorkSheet;

ExcelApp = new Excel.Application();
ExcelApp.Visible = true;
ExcelWorkbook = (Excel._Workbook)(ExcelApp.Workbooks.Add(
Missing.Value ));

ExcelWorkSheet = (Excel._Worksheet)ExcelWorkbook.Sheets["Sheet1"];
ExcelWorkSheet.Name = "My Sheet";
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
ExcelApp.Visible = true;
ExcelApp.UserControl = true;
//text in cell
ExcelWorkSheet.Cells[1, 1] = "test sheet";
ExcelWorkSheet.Cells[3, 1] = "test1";
ExcelWorkSheet.Cells[3, 2] = "test2";
ExcelWorkSheet.Cells[3, 3] = "test3";
ExcelWorkSheet.Cells[3, 4] = "test4";
ExcelWorkSheet.Cells[3, 5] = "test5";
ExcelWorkSheet.Cells[3, 6] = " test6";

ExcelWorkSheet.get_Range("A3", "F3").HorizontalAlignment =

ExcelWorkSheet.get_Range("A1", "A1").Font.Bold = true;
ExcelWorkSheet.get_Range("A3", "F3").EntireColumn.AutoFit();
ExcelWorkSheet.get_Range("A3",F3").BorderAround(Ex cel.XlLineStyle.xlContinuous,Excel.XlBorderWeight. xlMedium,
Excel.XlColorIndex.xlColorIndexAutomatic,Excel.XlC olorIndex.xlColorIndexAutomatic);

ExcelWorkSheet.get_Range("E1",E1").Columns.EntireC olumn.NumberFormat =

string savepath;
savepath = Application.ExecutablePath.ToString();
savepath = savepath.Substring(0,savepath.LastIndexOf("\\"));
savepath = savepath + "\\TestExcelFile.xls";

ExcelWorkbook.SaveAs(savepath,Excel.XlFileFormat.x lCSV,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange,T ype.Missing,Type.Missing,

Thanks again

I Decker
Nov 15 '05 #1
0 40826

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Martin | last post: by
4 posts views Thread by mattmerc | last post: by
27 posts views Thread by jeniffer | last post: by
reply views Thread by kennedystephen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.