By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

Excel SaveAs problem

P: n/a
Hi all,

I wrote a small app, which could read text file and fill the data from
text file to a newly created excel file. It works fine on my computer,
but did not succeed on someone's. I caught an exception which told it
occurred when "trying to read or write protected memory. It usually
means the memory was damaged". What did I need to check? The account is
administrator, and it should be fine to write file at any place in that
PC.

Here is a my code. Thanks for any replying.

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

void func1(string xlsfilename)
{
// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
return;
}

try
{
string filename = xlsfilename;
Excel.Workbook wb = ExcelObj.Workbooks.Add(Type.Missing);

// get the collection of sheets in the workbook
Excel.Sheets sheets = wb.Worksheets;
Excel.Worksheet worksheet;

worksheet = (Excel.Worksheet)sheets.get_Item(1);
worksheet.Name = "Data";

//do some data processing

wb.SaveAs( filename , Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
wb.Close(false, Type.Missing, false);
}
catch(Exception ex)
{
MessageBox.Show("Message: " + ex.Message + "\nSource: " + ex.Source
+ "\nTrack: "
+ ex.StackTrace);
}

ExcelObj.Workbooks.Close();
ExcelObj.Quit();
ExcelObj = null;

GC.Collect();
}

Sep 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I just copied your code in to one of my apps and it works fine. The most
likely issue I could think of is a mismatch between the version of Excel
you're referencing in the IDE compared to the version you're executing
against.

Sorry I can't be more help.

Steve
"wasishincar" <ad*********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
Hi all,

I wrote a small app, which could read text file and fill the data from
text file to a newly created excel file. It works fine on my computer,
but did not succeed on someone's. I caught an exception which told it
occurred when "trying to read or write protected memory. It usually
means the memory was damaged". What did I need to check? The account is
administrator, and it should be fine to write file at any place in that
PC.

Here is a my code. Thanks for any replying.

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

void func1(string xlsfilename)
{
// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
return;
}

try
{
string filename = xlsfilename;
Excel.Workbook wb = ExcelObj.Workbooks.Add(Type.Missing);

// get the collection of sheets in the workbook
Excel.Sheets sheets = wb.Worksheets;
Excel.Worksheet worksheet;

worksheet = (Excel.Worksheet)sheets.get_Item(1);
worksheet.Name = "Data";

//do some data processing

wb.SaveAs( filename , Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
wb.Close(false, Type.Missing, false);
}
catch(Exception ex)
{
MessageBox.Show("Message: " + ex.Message + "\nSource: " + ex.Source
+ "\nTrack: "
+ ex.StackTrace);
}

ExcelObj.Workbooks.Close();
ExcelObj.Quit();
ExcelObj = null;

GC.Collect();
}

Sep 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.