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 8872267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:10:58+00:00 2026-06-14T18:10:58+00:00

I created a excel helper class, to interact excel interop service. But i noticed

  • 0

I created a excel helper class, to interact excel interop service.
But i noticed that the excel.exe is not getting closed on the server.
(windows 2008 64bit Japanese OS and office 2007 32bit).
When i checked with process explorer it shows tooltip like:

Path:[Error opening process]

I did excel.Quit() and Marshal.FinalReleaseComObject(_xlApp) but nothing works as expected, then tried to kill the process by processID, still not killing the process.

uint processID = 0;
GetWindowThreadProcessId((IntPtr)_hWnd, out processID);
 if (processID != 0)
 {
  System.Diagnostics.Process.GetProcessById((int)processID).Kill();
 }

Then i tried below both method, but it close all manually opened excel documents.

System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("EXCEL");
        foreach (System.Diagnostics.Process p in procs)
        {
            int baseAdd = p.MainModule.BaseAddress.ToInt32();
            if (baseAdd == _xlApp.Hinstance)
            {
                p.Kill();
            }
        }

System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("EXCEL");
         foreach (System.Diagnostics.Process p in procs)
         {
             if (p.MainWindowTitle.Length == 0)
             {
                 p.Kill();
             }
         }

Any idea about how to deal with this case ?

  • 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-06-14T18:10:59+00:00Added an answer on June 14, 2026 at 6:10 pm

    To get the processId is a little bit more complicated.
    Try this…

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using Excel = Microsoft.Office.Interop.Excel;
    using Word = Microsoft.Office.Interop.Word;
    
        /// <summary>
        /// Gets an Interop.Application object and its associated processId
        /// </summary>
        /// <returns>Excel.Application or Word.Application depending on _isExcel</returns>
        private object ApplicationFactory()
        {
            object application = null;
            string processName = (_isExcel) ? "excel" : "winword";
            Process[] beforeProcesses = null;
            Process[] afterProcesses = null;
            int i = 0;
            while (i < 3)
            { // ourProcess = afterList - beforeList
                beforeProcesses = Process.GetProcessesByName(processName);
                application = (_isExcel) ? (object)new Excel.Application() : (object)new Word.Application();
                afterProcesses = Process.GetProcessesByName(processName);
                if ((afterProcesses.Length - beforeProcesses.Length) == 1)
                { // OK. Just a single new process
                    break;
                }
                else
                { // Two or more processes, we cannot get our processId
                    // therefore quit while we can and try again
                    if (_isExcel)
                        ((Excel._Application)application).Quit();
                    else
                        ((Word._Application)application).Quit();
                    int indexReferences = 1;
                    do
                    {
                        indexReferences = System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
                    }
                    while (indexReferences > 0);
                    application = null;
                    System.Threading.Thread.Sleep(150);
                    i++;
                }
            }
            if (application == null)
            {
                throw new ApplicationException("Unable to create Excel Application and get its processId");
            }
            List<int> processIdList = new List<int>(afterProcesses.Length);
            foreach (Process procDesp in afterProcesses)
            {
                processIdList.Add(procDesp.Id);
            }
            foreach (Process proc in beforeProcesses)
            {
                processIdList.Remove(proc.Id);
            }
            _processId = processIdList[0];
            return application;
        }
    
        /// <summary>
        /// Kills _processId process if exists
        /// </summary>
        private void ProcessKill()
        {
            Process applicationProcess = null;
            if (_processId != 0)
            {
                try
                {
                    applicationProcess = Process.GetProcessById(_processId);
                    applicationProcess.Kill();
                }
                catch
                { // no Process with that processId
                }
            }
        }
    

    That said, violence is just the last resourse 😉
    You need to kill because there are some COM objects not released. (See
    MS Support: Office application does not close)
    Try reference your COM objects always, put them in a Stack and release them after use with

    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
    

    then, a simple application.Quit();application = null will make the trick.

    Hope it helps.

    EDIT:
    – Always means: “whenever you use two points (_xlApp.Application., _xlWorkbook.Worksheets,...)

    • Add to stack means stack.push(_xlApp.Application)

    • Release means stack.pop()

    I include mi helperStack

    using System.Collections.Generic;
    
    namespace OfficeUtils.Stack
    {
    /// <summary>
    /// Stack of COM objects to be released
    /// </summary>
    public abstract class ComObjectsStack
    {
        private Stack<object> comObjects = new Stack<object>();
        private int mark = 0;
    
        /// <summary>
        /// Releases all the remaining COM objects
        /// </summary>
        ~ComObjectsStack()
        {
            if (comObjects.Count > 0)
                ReleaseAll();
            comObjects = null;
        }
    
        /// <summary>
        /// Add a new object to the stack to be released
        /// </summary>
        /// <param name="obj">Nuevo objeto a liberar</param>
        public void Push(object obj)
        {
            comObjects.Push(obj);
        }
    
        /// <summary>
        /// Release the last object in the stack
        /// </summary>
        public void Pop()
        {
            Release(1);
        }
    
        /// <summary>
        /// Mark for future use of ReleaseUpToMark
        /// </summary>
        public void Mark()
        {
            mark = comObjects.Count;
        }
    
        /// <summary>
        /// Release up to mark
        /// </summary>
        /// <returns>Number of released objects</returns>
        public int ReleaseUpToMark()
        {
            int numberObjects = comObjects.Count - mark;
            if (numberObjects > 0)
            {
                Release(numberObjects);
                return numberObjects;
            }
            else
            {
                return 0;
            }
        }
    
        /// <summary>
        /// Release all the objects in the stack
        /// </summary>
        public void ReleaseAll()
        {
            if (comObjects != null)
                Release(comObjects.Count);
        }
    
        /// <summary>
        /// Release the last numberObjects objects in stack
        /// </summary>
        /// <param name="numberObjects">Number of objects to release</param>
        private void Release(int numberObjects)
        {
            for (int j = 0; j < numberObjects; j++)
            {
                object obj = comObjects.Pop();
                int i = 1;
                do
                {
                    i = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                }
                while (i > 0);
                obj = null;
            }
        }
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a .csv file that was created in Excel with the first line
I created a GPA spreadsheet in excel and have the basics working but I
I have created a macro for excel which will pop up form that contains
I created a VB.Net application that will read from excel file and put the
I have 30 charts that were created from excel and were pasted onto powerpoint
I have created a tool that imports an excel sheet. The excel COM object
I have created an excel workbook application that is free to use; however, I
I have created an Excel file from my JTable. The same button that creates
I have created an excel worksheet in a such way that the user can
I have XML spreadsheet files created from Excel that I need to use as

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.