title: How do I get the Process ID from a created "Excel.Application" object?
body: <p>How do I get the Process ID from a running object?</p>
  
  <pre><code>Dim xlApp As Object  = CreateObject("Excel.Application")
  </code></pre>
  
  <p>I need to use late binding because I can't guarantee which version I will get so using <code>Microsoft.Office.Interop.Excel</code> won't work.</p>
  
  <pre><code>'do some work with xlApp
  
  xlApp.Quit
  System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
  xlApp = nothing
  </code></pre>
  
  <p>At this point Excel is still running in the background. I am familiar with all the recommendations to use variables and release them then use: <code>System.Runtime.InteropServices.Marshal.ReleaseComObject(o)</code>. This does not work reliably. The work that I'm doing is very complicated. I use for each loops etc using multiple files. It is impossible to release all of the resources in Excel.  I need a better option. </p>
  
  <p>I'd like to use <code>Process.Kill</code> on Excel but I don't know how to get the process from the xlApp object. I don't want to kill all Excel processes because the user might have a workbook open. </p>
  
  <p>I tried using <code>Dim xProc As Process = Process.Start(ExcelPath)</code> then using <code>xProc.Kill()</code> That works sometimes except it's a little tricky to get the correct Excel object using <code>XLApp = GetObject("Book1").Application</code> or <code>XLApp = GetObject("", "Excel.Application")</code> if the user already has Excel windows open. I need a better option.</p>
  
  <p>I can't use <code>GetActiveObject</code> or <code>BindToMoniker</code> to get the Excel object because they only work with work when using early binding. E.g. <code>Microsoft.Office.Interop.Excel</code></p>
  
  <p>How do I get the Process ID from a running object?</p>
  
  <p>Edit: Actually I'm not really interested in a rehash on how to get Excel to nicely exit. Many other questions have addressed that. <a href="https://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects" title="Here">here</a> and <a href="https://stackoverflow.com/questions/17130382/understanding-garbage-collection-in-net/17131389#17131389">here</a> I just want to kill it; cleanly, precisely and directly. I want to kill the exact process that I started and no other. </p>