none
If I create a workbook on my Excel COM AddIn, then Excel leaves a zombie process running RRS feed

  • Question

  • Hi all;

    I have an Excel AddIn. For one action I (code below) open another XLSX file, export it as PDF, then close it. At the completion of those actions there is 1 Excel process running according to the task manager.

    When I close Excel, that process does not exit. It continues running.

    If I start and exit Excel with my AddIn but do not do the create PDF, then when I close Excel, the process exits.

    What am I missing?

    thanks - dave

    		public static void Convert(Application excel, string inputFile, string outputFile)
    		{
    
    			Workbooks workbooks = null;
    			Workbook activeWorkbook = null;
    			Workbook workbook = null;
    
    			MsoFeatureInstall origFeatureInstall = MsoFeatureInstall.msoFeatureInstallNone;
    			bool[] origExcelOptions = new bool[7];
    			defaultExcelOptions.CopyTo(origExcelOptions, 0);
    
    			bool okToRestore = false;
    			try {
    				try {
    					origFeatureInstall = excel.FeatureInstall;
    
    					//origExcelOptions[0] = excel.Visible;
    					origExcelOptions[1] = excel.DisplayAlerts;
    					origExcelOptions[2] = excel.AskToUpdateLinks;
    					origExcelOptions[3] = excel.AlertBeforeOverwriting;
    					origExcelOptions[4] = excel.EnableLargeOperationAlert;
    					origExcelOptions[5] = excel.Interactive;
    					origExcelOptions[6] = excel.EnableEvents;
    
    					okToRestore = true;
    
    					excel.ScreenUpdating = false;
    					excel.FeatureInstall = MsoFeatureInstall.msoFeatureInstallNone;
    
    					SetExcelOptions(excel, silentExcelOptions);
    				}
    				catch (Exception) {
    					Trap.trap();
    				}
    
    				workbooks = excel.Workbooks;
    				activeWorkbook = excel.ActiveWorkbook;
    
    				object oMissing = System.Reflection.Missing.Value;
    				workbook = workbooks.Open(inputFile, true, false, oMissing, oMissing, oMissing, true, oMissing, oMissing,
    										 oMissing, oMissing, oMissing, false, oMissing, oMissing);
    
    				workbook.RunAutoMacros(XlRunAutoMacro.xlAutoOpen);
    
    				XlFixedFormatQuality quality = XlFixedFormatQuality.xlQualityStandard;
    				workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,
    											 outputFile, quality, true, false, Type.Missing, Type.Missing, false, Type.Missing);
    				workbook.Close(false, oMissing, oMissing);
    				activeWorkbook.Activate();
    			}
    
    			// options back to original, COM objects released
    			finally {
    				try {
    					if (okToRestore) {
    						excel.ScreenUpdating = true;
    						excel.FeatureInstall = origFeatureInstall;
    						SetExcelOptions(excel, origExcelOptions);
    					}
    				}
    				catch (Exception) {
    					Trap.trap();
    				}
    				try {
    					if (workbook != null)
    						Marshal.ReleaseComObject(workbook);
    					if (activeWorkbook != null)
    						Marshal.ReleaseComObject(activeWorkbook);
    					if (workbooks != null)
    						Marshal.ReleaseComObject(workbooks);
    
    				}
    				catch (Exception) {
    					Trap.trap();
    				}
    			}
    		}
    
    		private static void SetExcelOptions(Application excel, bool[] options)
    		{
    			//excel.Visible = options[0];
    			excel.DisplayAlerts = options[1];
    			excel.AskToUpdateLinks = options[2];
    			excel.AlertBeforeOverwriting = options[3];
    			excel.EnableLargeOperationAlert = options[4];
    			excel.Interactive = options[5];
    			excel.EnableEvents = options[6];
    		}


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Sunday, October 30, 2016 3:53 PM

All replies

  • Hi Davidthi808,

    I made a test with your part code with Excel VSTO add-in, it could not be reproduced. It seems it is related with com add-in. At present, there is no com add template in available under VS 2013. Could you share us how you create your com add in? A detailed steps would be much helpful.

    In addition, I suggest you check whether your issue exist under Excel VSTO add-in.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 31, 2016 5:11 AM
  • Hi;

    Unfortunately we can't use VSTO because of its limitations compared to the COM API.

    I think this page shows how to create a COM AddIn. (Here's another.) I also have a very basic C# solution that creates a simple AddIn if you want that. Please let me know and I'll upload it.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Monday, October 31, 2016 11:15 AM
  • Hi DaviDThi,

    I checked your first link, it seems this is a VSTO Add-in by Office VSTO Template in VS. As my understanding for Com add-in, Com add-ins must implement each of the five methods of this interface: OnConnection, OnStartupComplete, OnAddinsUpdate, OnBeginShutDown, and OnDisconnection.

    # Excel COM add-ins and Automation add-ins

    https://support.microsoft.com/en-us/kb/291392

    If I misunderstood you, please feel free to let me know, and a simple demo which could reproduce your issue would be much helpful.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 1, 2016 3:11 AM
  • Hi Edward;

    I'm sorry, you're right. Please download BasicComAddIn.zip - it's a very small AddIn I created. Also, this link also has similar resources.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Tuesday, November 1, 2016 11:40 AM
  • Hi DavidThi808,

    I download your file, run VS as administrator, open this file, change the start external program path, crate test folder under C:, debug this project, but I failed to see any ribbon. Could you share me detailed steps to make your project run?

    Sorry for less help.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 2, 2016 9:49 AM
  • Hi DavidThi,

    Currently, I could make a test with Com add-in, I could not make the same code as yours work for some method is not shared.

    I made a test with below code, but I failed to reproduce your issue. The Excel process exits after I close Excel application.

    Here is my test.

            public static void Convert(Application excel, string inputFile, string outputFile)
            {
    
                Workbooks workbooks = null;
                Workbook activeWorkbook = null;
                Workbook workbook = null;
    
                MsoFeatureInstall origFeatureInstall = MsoFeatureInstall.msoFeatureInstallNone;
                bool[] origExcelOptions = new bool[7];
                //defaultExcelOptions.CopyTo(origExcelOptions, 0);
    
                bool okToRestore = false;
                try
                {
                    try
                    {
                        origFeatureInstall = excel.FeatureInstall;
    
                        //origExcelOptions[0] = excel.Visible;
                        origExcelOptions[1] = excel.DisplayAlerts;
                        origExcelOptions[2] = excel.AskToUpdateLinks;
                        origExcelOptions[3] = excel.AlertBeforeOverwriting;
                        origExcelOptions[4] = excel.EnableLargeOperationAlert;
                        origExcelOptions[5] = excel.Interactive;
                        origExcelOptions[6] = excel.EnableEvents;
    
                        okToRestore = true;
    
                        excel.ScreenUpdating = false;
                        excel.FeatureInstall = MsoFeatureInstall.msoFeatureInstallNone;
    
                        //SetExcelOptions(excel, silentExcelOptions);
                    }
                    catch (Exception)
                    {
                        //Trap.trap();
                    }
    
                    workbooks = excel.Workbooks;
                    activeWorkbook = excel.ActiveWorkbook;
    
                    object oMissing = System.Reflection.Missing.Value;
                    workbook = workbooks.Open(inputFile, true, false, oMissing, oMissing, oMissing, true, oMissing, oMissing,
                                             oMissing, oMissing, oMissing, false, oMissing, oMissing);
    
                    workbook.RunAutoMacros(XlRunAutoMacro.xlAutoOpen);
    
                    XlFixedFormatQuality quality = XlFixedFormatQuality.xlQualityStandard;
                    workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,
                                                 outputFile, quality, true, false, Type.Missing, Type.Missing, false, Type.Missing);
                    workbook.Close(false, oMissing, oMissing);
                    activeWorkbook.Activate();
                }
    
                // options back to original, COM objects released
                finally
                {
                    try
                    {
                        if (okToRestore)
                        {
                            excel.ScreenUpdating = true;
                            excel.FeatureInstall = origFeatureInstall;
                            SetExcelOptions(excel, origExcelOptions);
                        }
                    }
                    catch (Exception)
                    {
                        //Trap.trap();
                    }
                    try
                    {
                        if (workbook != null)
                            Marshal.ReleaseComObject(workbook);
                        if (activeWorkbook != null)
                            Marshal.ReleaseComObject(activeWorkbook);
                        if (workbooks != null)
                            Marshal.ReleaseComObject(workbooks);
    
                    }
                    catch (Exception)
                    {
                       // Trap.trap();
                    }
                }
            }
    
            private static void SetExcelOptions(Application excel, bool[] options)
            {
                //excel.Visible = options[0];
                excel.DisplayAlerts = options[1];
                excel.AskToUpdateLinks = options[2];
                excel.AlertBeforeOverwriting = options[3];
                excel.EnableLargeOperationAlert = options[4];
                excel.Interactive = options[5];
                excel.EnableEvents = options[6];
            }
    

    I suggest you make a simple add-in to test above code to check whether this issue exists. If your issue exist in your code, but not in my above code, I would suggest you share us the complete code.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 9, 2016 10:06 AM
  • Hi;

    Your example does not leave a zombie process running. Mine does - sometimes. It may be that Excel is buggy this way.


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Wednesday, November 9, 2016 12:53 PM
  • Hi DavidThi,

    Do you mean you test my code, and it did not leave a zombie process running? If so, I assume it might be related with code in other parts. If you insist on this issue, I would suggest you share us the complete code which could reproduce your issue.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 10, 2016 9:05 AM