Click here to Skip to main content
13,151,514 members (43,111 online)
Rate this:
Please Sign up or sign in to 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.

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;

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.
excelApp.ActiveWorkbook.Close((Object)false, (Object)oMissing, (Object)oMissing);

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 30-Dec-10 20:32pm
Updated 30-Dec-10 20:45pm
Espen Harlinn 31-Dec-10 6:17am
Why aren't you using excelWorkbook object reference for saving?
Manfred R. Bihy 31-Dec-10 8:04am
@Espen: Good question!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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


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,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

<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>

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170924.2 | Last Updated 17 May 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100