Consider this code:
using Microsoft.Office.Interop.Word;
ApplicationClass _application = new ApplicationClass();
Can I get the PID from the Winword.exe process that was launched by the _application?
I need the PID because with corrupted files, I just can't quit the ApplicationClass, even using this code:
_application.Quit(ref saveFile, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_application);
GC.Collect();
GC.WaitForPendingFinalizers();
I can't search for the winword.exe process and kill it, because I will have several, and I don't know which one to kill. If I can get a PID for each ApplicationClass, I could just kill the correct winword.exe process that is giving me troubles to quit.
Click the File tab to go to the Backstage area. In the left navigation panel, click the Info category. On the right side of the screen, click the Properties drop-down and choose Show Document Panel. There you have it, four simple clicks to open the Document Information Panel in Word (or Excel or PowerPoint).
On the Review tab, select Track Changes. In the Track Changes drop-down list, select one of the following: To track only the changes that you make to the document, select Just Mine. To track changes to the document made by all users, select For Everyone.
Here is how to do it.
//Set the AppId
string AppId = ""+DateTime.Now.Ticks(); //A random title
//Create an identity for the app
this.oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
this.oWordApp.Application.Caption = AppId;
this.oWordApp.Application.Visible = true;
while (GetProcessIdByWindowTitle(AppId) == Int32.MaxValue) //Loop till u get
{
Thread.Sleep(5);
}
///Get the pid by for word application
this.WordPid = GetProcessIdByWindowTitle(AppId);
///You canh hide the application afterward
this.oWordApp.Application.Visible = false;
/// <summary>
/// Returns the name of that process given by that title
/// </summary>
/// <param name="AppId">Int32MaxValue returned if it cant be found.</param>
/// <returns></returns>
public 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.Equals(AppId))
{
return P_CESSES[p_count].Id;
}
}
return Int32.MaxValue;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With