<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>crunchlife: And finally, kill Excel...</title>
    <link>http://crunchlife.com/articles/2009/01/14/and-finally-kill-excel</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>And finally, kill Excel...</title>
      <description>&lt;p&gt;Everyone who&amp;#8217;s worked with Excel on the Microsoft .NET framework has dealt with the problem of hanging Excel processes. The C# solution below is not very elegant, but it stops those pesky Excel processes from hanging around.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;using System.Diagnostics;
using System.Runtime.InteropServices;

[DllImport(&amp;quot;user32.dll&amp;quot;)]
private static extern
    int GetWindowThreadProcessId(int hWnd, out int processId);

private static void CreateSpreadsheet()
{
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

    try
    {
      // Create Excel spreadsheet.
    }
    catch (Exception ex)
    {
      // Do something with the exception.
    }
    finally
    {
      KillExcel(excel.hWnd);
    }
}

private static void KillExcel(int hWnd)
{
    int processId;
    int threadProcessId = GetWindowThreadProcessId(hWnd, out processId);

    Process.GetProcessById(processId).Kill();
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 14 Jan 2009 20:07:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:48876e48-1801-44ba-a6d2-73902582a453</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2009/01/14/and-finally-kill-excel</link>
      <category>Code Snippets</category>
      <category>CSharp</category>
      <category>dotNET</category>
    </item>
    <item>
      <title>"And finally, kill Excel..." by Ryan Baxter</title>
      <description>Thanks! My solution will only work with Excel 2003 and above. Check out &lt;a href="http://www.devcity.net/Articles/239/1/article.aspx" rel="nofollow"&gt;50 Ways to Kill Excel&lt;/a&gt; for a more detailed process. </description>
      <pubDate>Thu, 19 Feb 2009 06:48:32 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:ab297a2c-1b2d-4174-ae7d-eddef081f177</guid>
      <link>http://crunchlife.com/articles/2009/01/14/and-finally-kill-excel#comment-24599</link>
    </item>
    <item>
      <title>"And finally, kill Excel..." by Alex</title>
      <description>Brilliant, thank you!</description>
      <pubDate>Thu, 19 Feb 2009 05:15:29 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:05266751-2afa-40a3-a6ea-67c6264eef9f</guid>
      <link>http://crunchlife.com/articles/2009/01/14/and-finally-kill-excel#comment-24593</link>
    </item>
  </channel>
</rss>
