Excel not closing at end of Excel Application Scope, Close Workbook

I am having a periodic issue where following the end of an Excel Application Scope activity it doesn’t close Excel. I have added a Close Workbook activity following the end of the application scope and it doesn’t help. It also will never time out. I have to go into Task Manager, kill the Excel process and the workflow continues.
This doesn’t happen every time but it is not uncommon. (~10%-20%).

I ended up putting in a Kill Process activity in another workflow to address the issue but do not like that solution as that sometimes leaves Windows thinking that the file is still open and it cannot be reopened.

Studio 19.10.1

Hi @russell.arnold,
Welcome to the Community!
It might be rather something with excel but additionally I see you are using quite old Studio version. You might try the newest one and see if there is still the same issue. Or you can use try catch activity where you will try to close excel and if this will fail you could close it using for example powershell or some service-related activities.

You have to use kill process activity

Hi, I have the same issue with sharepoint file but I fixed it just by start and login to One Drive

Hey @russell.arnold ,

The issue you’re facing with Excel not closing after the Excel Application Scope is something I’ve seen before. It’s not uncommon, especially with older versions of UiPath Studio (19.10.1) and certain system configurations. Here are some potential solutions to ensure Excel closes properly:

Solution 1: Use the Excel Application Scope Property

  • Ensure the “Visible” property of the Excel Application Scope is set to False. This reduces the chances of Excel staying open in the background.
  • Enable the “AutoSave” property and set the “CloseWorkbook” property to True.

Solution 2: Use Close Workbook with Delay

Sometimes, adding a short delay before the Close Workbook activity can help:

  1. Add a Delay activity with a short duration (e.g., 00:00:02).
  2. Place it right before the Close Workbook activity.

Solution 3: Implement Kill Process with Conditions

If the issue persists, you can use a conditional Kill Process activity:

  1. Use a Kill Process activity with the following settings:
    • ProcessName: "EXCEL"
  2. Wrap this in a Try-Catch block to handle any potential issues gracefully.
  3. Add a check before Kill Process to ensure Excel is not already closed. Use:
    Process.GetProcessesByName("EXCEL").Any()
    

Solution 4: Use Release COM Objects in Invoke Code

If Excel processes are still not closing properly, try releasing COM objects manually:

  1. Add an Invoke Code activity at the end of your workflow with this code:
    System.Runtime.InteropServices.Marshal.ReleaseComObject(yourWorkbookVariable)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(yourExcelAppVariable)
    GC.Collect()
    GC.WaitForPendingFinalizers()
    
    Make sure to replace yourWorkbookVariable and yourExcelAppVariable with your actual variables.

Solution 5: Upgrade to a Newer Version of UiPath Studio

  • Your UiPath Studio version (19.10.1) is quite old. Consider upgrading to a more recent version where this issue is less likely to occur. The newer versions have improved Excel handling and stability.

Summary of Best Practices:

  • Always set "Visible" = False in Excel Application Scope.
  • Use Close Workbook followed by a short delay.
  • Implement a conditional Kill Process as a last resort.
  • Consider releasing COM objects if Excel is used extensively.

I hope one of these solutions resolves your issue. Let me know if you need further assistance! :blush:

Best,
Chaitanya