Click here to Skip to main content
15,902,893 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,

I have a situation where I need to save current active document opened with my project. e.g. When I open Word or Excel file and I will do some changes into the same document. Now I want to save the document.

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

//This will gives you the Active work book name
//excelApp.Application.ActiveWorkbook.Name;

//This will save the original document...
excelApp.ActiveWorkbook.Save();

in above code I am not getting the exact active workbook so that I can save it properly. It is showing null active workbook.

Is there any other way to save already opened DOCX, ELSX & PPTX document using C#?

Thanks & Regards,
Balkrishna Raut
Posted
Updated 20-Jan-11 5:02am
v3

C#
// Create Excel app
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

// Open Excel file
excelApp.Workbooks.Open("c:\\test\\test.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

// Find the active sheet
Microsoft.Office.Interop.Excel.Worksheet sheet = excelApp.ActiveWorkbook.ActiveSheet;

// Get a range to modify
Microsoft.Office.Interop.Excel.Range range = sheet.get_Range("A1", Type.Missing);

// Set a value for this range
range.set_Value(Type.Missing, "test value");

// Close and save the file
excelApp.ActiveWorkbook.Close(true, Type.Missing, Type.Missing);

// Close Excel
excelApp.Quit();
 
Share this answer
 
v3
Hi,

"DizZ" answered to your question, but I want to add something. Maybe it will be useful to you sometimes. ;)

Look for OpenXML SDK! Here is the download link: http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0[^]

With it you can do what ever you want with any office document (docx,xlsx, pptx) in "OOP" way.
Using it you don't .
Working with it life is so much easier then using Office (excel word) interoperability. :-D
 
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