Thanks Tom and Colin for your replies.
Colin - I have posted my code so far as below. Am using Visual C# 2008 and Visual Studio 2008.
Tom - yes this is something that I have come across before, and may well be the case. I have tried to get it to register the apps in the past ie once the first one is closed to move on to the next one, but wasn't able to get it to move on. This was mainly because the first Excel app still existed in memory and showed up on task manager even when the application is quit. How would you recommend changing the code below / suggestions to refer to the workbooks in the first Excel application and then referring to workbooks on the next Excel application?
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.ComponentModel;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace Officedocs
{
class Excelproj
{
public static void ExcelClass()
{
Excel.Application oExcelApp = null;
try
{
//To get reference to the Excel app
oExcelApp = (Excel.Application)System.Runtime
.InteropServices.Marshal.GetActiveObject("Excel.Application");
oExcelApp.DisplayAlerts = false; //don't display updates
Process[] processlist = Process.GetProcessesByName("Excel"); //Shows number of running Excel apps
foreach (Process theprocess in processlist) //foreach Excel app running
{
if (oExcelApp.Workbooks.Count >= 0) //for worbooks in each Excel app
{
foreach (Excel.Workbook wkb in oExcelApp.Application.Workbooks)
{
//Save files using their own names in the specified folder
Object oSaveAsFileExcel1 = 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);
wkb.Close(true, null, null);
//Release the wbk object
Marshal.FinalReleaseComObject(wkb); //Release the Excel wkb object
}
//Close workbooks
oExcelApp.Workbooks.Close();
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
oExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcelApp);
}
return;
}
catch //(Exception x)
{ }
}
}
}