Given the following code (console application C#) the Word application stops after approximately 300-400 milliseconds, however the Excel application keeps on running. I can't find a way to stop it (without killing the process).
Any suggestions?
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Diagnostics;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;
using System.Collections.Generic;
namespace test
{
class Program
{
private static int GetProcessIdByWindowTitle(string AppId)
{
Process[] P_CESSES = Process.GetProcesses();
for (int p_count = 0; p_count < P_CESSES.Length; p_count++)
{
if (P_CESSES[p_count].MainWindowTitle.IndexOf(AppId) != -1)
{
return P_CESSES[p_count].Id;
}
}
return Int32.MaxValue;
}
static void Excel()
{
// create the MS-Excel Application
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
// Set the appID
string appID = "zizozazezu";
excelApp.Application.Caption = appID;
excelApp.Application.Visible = true;
// Excel needs a workbook to sete a caption visible...
Microsoft.Office.Interop.Excel.Workbooks wbs = excelApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add();
// find the
int processID = GetProcessIdByWindowTitle(appID);
// verified this is the pid of MS-Excel
Console.WriteLine(processID);
// bailing out, no save
object saveChanges = false;
((Microsoft.Office.Interop.Excel._Workbook)wb).Close(saveChanges);
((Microsoft.Office.Interop.Excel._Application)excelApp).Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;
Console.WriteLine("Excel is done...");
for (int i=0; i<100; i++)
{
// Patience....
Thread.Sleep(100);
// still running???
bool isRunning = false;
try
{
Process p = Process.GetProcessById(processID);
if (!p.HasExited)
{
isRunning = true;
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
if (isRunning)
{
Console.Write(".");
}
else
{
Console.WriteLine("Excel done at i=" + i.ToString());
return;
}
}
return;
}
static void Word()
{
// create the MS-Word Application
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
// Set a caption - find the right instance
string appID = "ladadidada";
wordApp.Caption = appID;
wordApp.Visible = true;
// find the
int processID = GetProcessIdByWindowTitle(appID);
// verified this is the pid of MS-Word
Console.WriteLine(processID);
// Stop MS-Word
object saveChanges = false;
((Microsoft.Office.Interop.Word._Application)wordApp).Quit(ref saveChanges);
for (int i=0; i<100; i++)
{
// Patience....
Thread.Sleep(100);
// still running???
bool isRunning = false;
try
{
Process p = Process.GetProcessById(processID);
if (!p.HasExited)
{
isRunning = true;
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
if (isRunning)
{
Console.Write(".");
}
else
{
Console.WriteLine("Word done at i=" + i.ToString());
return;
}
}
return;
}
static int Main(string[] args)
{
Word();
Excel();
return -1;
}
}
}