Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Identifying anc saving workbooks from more than one Excel applicat

Hi,

I am trying to automate saving and shutting down Excel applications. At the
moment, this is saving and closing workbooks in applications, as I can't see
a way to directly save applications. The problem I have is that when I get
the current Excel application using GetActive object, it seems to either hold
on to this permantly (until the program finishes) or not attach to the next
open Excel application once I save and close down the first.

My code is as below, so any help to enable saving and closing each open
application would be appreciated using a loop, etc. Also this doesn't work
if one of the cells in any of the workbooks in the application is being
worked on ie if someone starts entering numbers, letters, etc in a cell but
doesn't hit return - any ideas on this too?

class Excelfile4
{

public static void ExcelClass4(string datestring, object dirString,
string timestring)
{
try
{

Object oMissing = System.Reflection.Missing.Value;
Object oTrue = true;
Object oFalse = false;

Process[] processlist = Process.GetProcessesByName("Excel");
//Shows number of running Excel apps
foreach (Process theprocess in processlist) //foreach Excel
app running
{
wkbs1.wkbs2(theprocess);
System.Threading.Thread.Sleep(500);
theprocess.Kill();
}
}

catch (Exception e)
{
string error = e.Message;
}

finally
{
GC.Collect();
GC.WaitForPendingFinalizers();

GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
}

class wkbs1
{
public static void wkbs2(Process theprocess)
{
DateTime nowdate = DateTime.Now;
string datestring = nowdate.Date.ToLongDateString();
string tempstring = nowdate.ToShortTimeString(); //use to create
directory with time stamp
string timestring = tempstring.Replace(":", "_"); //replace the
colon with an underscore
string dirSt = "C:\\" + datestring;

Excel.Application oExcelApp = (Excel.Application)System.Runtime

..InteropServices.Marshal.GetActiveObject("Excel.A pplication");

if (oExcelApp.Workbooks.Count = 0)
{
foreach (Excel.Workbook wkb in oExcelApp.Workbooks)
{
oExcelApp.DisplayAlerts = false; //Turn display alert off

//Save files using their own names in the specified folder
Object oSaveAsFileExcel1 = dirSt + @"\" + timestring + "
Copy of " + wkb.Name;

//Save each workbook
wkb.SaveAs(oSaveAsFileExcel1, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange,
XlSaveConflictResolution.xlLocalSessionChanges,
true, Type.Missing, Type.Missing, Type.Missing);

//Release the wbk object
Marshal.ReleaseComObject(wkb); //Release the Excel wkb object
System.Threading.Thread.Sleep(500);
}
}

//Close active workbook so as to move on to the next one
System.Threading.Thread.Sleep(500);
oExcelApp.Workbooks.Close();
System.Threading.Thread.Sleep(500);
oExcelApp.Quit();

return;
}
}

Regards,

Stephen
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
How to restric another excel app to attach to the created applicat Alamelu Excel Programming 3 March 25th 09 09:40 AM
How can i display excel file in winforms in Smart Device Applicat Bhavesh Mandaliya Excel Discussion (Misc queries) 0 October 20th 08 07:11 AM
Identifying workbooks with links to external file Barb Reinhardt Excel Programming 3 February 28th 06 09:31 PM
Closing excel and saving workbooks Hari[_3_] Excel Programming 2 June 10th 04 12:08 AM
saving excel workbooks Neil Benn Excel Programming 0 November 10th 03 05:19 PM


All times are GMT +1. The time now is 08:33 AM.

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

About Us

"It's about Microsoft Excel"