Click here to Skip to main content
11,574,494 members (56,456 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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;
 
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.
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 30-Dec-10 20:32pm
Edited 30-Dec-10 20:45pm
v2
Comments
Espen Harlinn at 31-Dec-10 6:17am
   
Why aren't you using excelWorkbook object reference for saving?
Manfred R. Bihy at 31-Dec-10 8:04am
   
@Espen: Good question!
Rate this: bad
good
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.

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

Cheers,

Manfred
  Permalink  
v2
Comments
meBalkrishna at 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
Rate this: bad
good
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>
  Permalink  

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web04 | 2.8.150624.2 | Last Updated 17 May 2011
Copyright © CodeProject, 1999-2015
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