Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear all,
I am working on windows application. In this application i am working office 2007. i can open .xlsx file using following code.

C#
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

excelApp.Visible = true;

Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(varFileName,0, false, 5, """, """, false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, """,
                true, false, 0, true, false, false);
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkbook.ActiveSheet;

workSheet.Activate();

After that I will do some modification on same xlsx file.

Now problem start with, when I want to save the file.
I am doing this using following code.
C#
excelApp.ActiveWorkbook.Save();
excelApp.ActiveWorkbook.Close((Object)false, (Object)oMissing, (Object)oMissing);
excelApp.Quit();

It shows exception as Object reference not set to an instance of an object. It will not show any active document. It shows ActiveWorkbook=null;
Any kind of link, suggestion and specially expert advice would be highly appreciated.

Thanks & Regards,
Balkrishna Raut
Posted
Updated 30-Dec-10 20:45pm
v2
Comments
Espen Harlinn 31-Dec-10 6:17am    
Why aren't you using excelWorkbook object reference for saving?
Manfred Rudolf Bihy 31-Dec-10 8:04am    
@Espen: Good question!

Is there a function like Activate() for the workbook. Then You probably need to call excelWorkbook.Activate() to make that workbook the active one.

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.activate(v=VS.90).aspx[^]

Cheers,

Manfred
 
Share this answer
 
v2
Comments
meBalkrishna 31-Dec-10 6:50am    
Thanks a lot.

actually the issue was with
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

it needs to declare it as public variable.
so it can locate active worksheet.

Thanks & Regards,
Balkrishna
<small><big><strike><u><i><b><pre><code>&lt;&gt;&amp;<a href=""></a><a href=""></a>[<a href="" target="_blank"></a>]</code></pre></b></i></u></strike></big></small>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900