Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4335368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:39:36+00:00 2026-05-21T10:39:36+00:00

I have a windows service that opens up an Excel spreadsheet via the Microsoft.Office.Interop.Excel.Application

  • 0

I have a windows service that opens up an Excel spreadsheet via the Microsoft.Office.Interop.Excel.Application object.

Application xlApp = new Application();
Workbook workbook = xlApp.Workbooks.Open(fileName, 2, false);
...
...
workbook.Close();
xlApp.Quit();

I would like to kill the EXCEL.exe process that is left running after it is done working with the workbook.

I’ve tried the following with no success…

// This returns a processId of 0
IntPtr processId;
GetWindowThreadProcessId(new IntPtr(xlApp.Hwnd), out processId);
Process p = Process.GetProcessById(processId.ToInt32());   
p.Kill();

Anyone have any ideas as to how I can do this via a Windows Service?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:39:36+00:00Added an answer on May 21, 2026 at 10:39 am

    After much reading and frustration I’ve found a solution!

    All credit goes to dotNetkow, nightcoder and Mike Rosenblum for their solutions on this post: How do I properly clean up Excel interop objects?

    Here is what I did…

    1. Changed build mode of the project to “Release” (in DEBUG mode, COM objects have a hard time disposing of their references.


    2. Removed all double dot expressions (all COM objects should be tied to a variable so they can be released)

    3. Calling GC.Collect(), GC.WaitForPendingFinalizers(), and Marshal.FinalReleaseComObject() explicitly in a finally block

    Here is the acutal code I am using:

    Application xlApp = null;
    Workbooks workbooks = null;
    Workbook workbook = null;
    Worksheet sheet = null;
    Range r = null;
    object obj = null;
    
    try
    {
        xlApp = new Application();
        xlApp.DisplayAlerts = false;
        xlApp.AskToUpdateLinks = false;
        workbooks = xlApp.Workbooks;
        workbook = workbooks.Open(fileName, 2, false);
        sheet = workbook.Worksheets[1];
    
        r = sheet.get_Range("F19");
        obj = r.get_Value(XlRangeValueDataType.xlRangeValueDefault);
    }
    finally
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        if (value != null) Marshal.FinalReleaseComObject(value);
        if (r != null) Marshal.FinalReleaseComObject(r);
        if (sheet != null) Marshal.FinalReleaseComObject(sheet);
        if (workbooks != null) Marshal.FinalReleaseComObject(workbooks);
        if (workbook != null)
        {
            workbook.Close(Type.Missing, Type.Missing, Type.Missing);
            Marshal.FinalReleaseComObject(workbook);
        }
        if (xlApp != null)
        {
            xlApp.Quit();
            Marshal.FinalReleaseComObject(xlApp);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So clients upload Excel files to us and we have a windows service that
I have written a Windows Service, that opens up a WCF service on port
I have a Windows Service that hangs when opening an OLEDB-connection to an Excel-file,
I have a windows service that opens a WCF endpoint for client connections. This
I have a windows service application that runs as Local System, and I want
Hi All, I have an application (windows service/C#) which starts EXCEL and POWERPOINT and
I have a windows service that runs various system monitoring operations. However, when running
I have a windows service that runs fine, but I have to have it
I have a Windows service that runs implementations of a framework across multiple threads.
I have a windows service that has a custom configuration section. In the configSectionHandler

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.