Excel SaveAs problem

W

wasishincar

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();
}
 
S

Steve Barnett

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top