Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Programmatic saving of an existing excel file

I want to use C# to save the changes made to an existing excel file. The path
of the excel file is fixed and it can be assumed that the file is opened in
an excel window from before. All I want to do is write a program that
periodically saves whatever changes were made to the file (in the excel
window) since it was last saved. I have tried sending the Ctrl-S windows
message to excel and also tried playing around the
Microsoft.Office.Interop.Excel namespace, but to no avail. Is there a way to
do this that anyone is aware of? I am using Office 2007 but a solution for
Office 2003 or 2002 will also be of help to me.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Programmatic saving of an existing excel file

All you need to do is

XLApp.ActiveWorkbook.Save;

where XLApp is your reference variable to the Excel application. For
periodic saving, create a timer object, set its interval to 5 minutes
(or whatever interval you want) and then in the event code use the
line above to save the workbook. This assumes that the workbook has
been saved at least once to disc so that it has a path name. Or, you
could expand the code with

if (XLApp.ActiveWorkbook.Path.ToString.Length == 0)
{
XLApp.ActiveWorkbook.SaveAs "C:\Test\Book1.xls";
}
else
{
XLApp.ActiveWorkbook.Save;
}


If a workbook has never been saved, the Path property has a Length of
0, so you need to save it to a file. If the workbook has been saved at
least once, the Path property has a Length 0, so you can just Save.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Wed, 28 Jan 2009 00:16:05 -0800, Nikhil
wrote:

I want to use C# to save the changes made to an existing excel file. The path
of the excel file is fixed and it can be assumed that the file is opened in
an excel window from before. All I want to do is write a program that
periodically saves whatever changes were made to the file (in the excel
window) since it was last saved. I have tried sending the Ctrl-S windows
message to excel and also tried playing around the
Microsoft.Office.Interop.Excel namespace, but to no avail. Is there a way to
do this that anyone is aware of? I am using Office 2007 but a solution for
Office 2003 or 2002 will also be of help to me.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Programmatic saving of an existing excel file

Thanks for response Chip.

However, I have another question: Suppose that excel.exe is already running
before I create the object XLApp. How can I dynamically attach XLApp to the
already running excel.exe process, so that I am able to save an excel
workbook which had been opened from before? Immediately after creating XLApp,
the number of workbooks in it are zero which means that we have to manually
attach it to a running process. Probably I am missing out a simple statement
here but this is where I am stuck as of now.


"Chip Pearson" wrote:

All you need to do is

XLApp.ActiveWorkbook.Save;

where XLApp is your reference variable to the Excel application. For
periodic saving, create a timer object, set its interval to 5 minutes
(or whatever interval you want) and then in the event code use the
line above to save the workbook. This assumes that the workbook has
been saved at least once to disc so that it has a path name. Or, you
could expand the code with

if (XLApp.ActiveWorkbook.Path.ToString.Length == 0)
{
XLApp.ActiveWorkbook.SaveAs "C:\Test\Book1.xls";
}
else
{
XLApp.ActiveWorkbook.Save;
}


If a workbook has never been saved, the Path property has a Length of
0, so you need to save it to a file. If the workbook has been saved at
least once, the Path property has a Length 0, so you can just Save.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Wed, 28 Jan 2009 00:16:05 -0800, Nikhil
wrote:

I want to use C# to save the changes made to an existing excel file. The path
of the excel file is fixed and it can be assumed that the file is opened in
an excel window from before. All I want to do is write a program that
periodically saves whatever changes were made to the file (in the excel
window) since it was last saved. I have tried sending the Ctrl-S windows
message to excel and also tried playing around the
Microsoft.Office.Interop.Excel namespace, but to no avail. Is there a way to
do this that anyone is aware of? I am using Office 2007 but a solution for
Office 2003 or 2002 will also be of help to me.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seeing existing file names when saving jkiser Excel Discussion (Misc queries) 1 December 12th 08 06:27 PM
Excel Programmatic (Existing) Workbook Access in .NET 2.0 JumpingMattFlash Excel Programming 0 April 25th 08 09:20 AM
Adding date to file name when saving and over writing a existing f Nils Titley Excel Programming 10 March 22nd 08 04:50 PM
Programmatic indication of file to be PROCESSED. Hari Prasadh Excel Programming 11 February 1st 05 02:54 PM
Saving over an existing file (updating an HTM file) No Name Excel Programming 2 December 3rd 03 06:19 PM


All times are GMT +1. The time now is 12:06 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"