web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Excel Task remains run...
Power Automate
Answered

Excel Task remains running after Close Excel Command completed

(0) ShareShare
ReportReport
Posted on by 20

Hi. I have multiple PAD flows running that work with excel spreadsheets. If the spreadsheets are saved only in a local folder, then closing excel command completes fine but if the spreadsheet is saved to a synced sharepoint folder then close excel command completes ok in PAD but I get errors in subsequent runs as the excel task is still running (Task Manager confims it is still running but using 0% memory etc). If i stop the task running manually all works well again. 

I do need the spreadsheets to be synced as I access them from elsewhere too. I could move the spreadsheets to dedicated local file and then add a later step in the relevant PAD flows to copy the updated file to sharepoint but was wondering if anyone clever has worked out how to properly close excel when working with sharepoint located files?

 

I have the same question (0)
  • Verified answer
    VJR Profile Picture
    7,635 on at

    Hi @JGIT 

     

    Try killing/terminating Excel.exe process after the first work is done and before reusing Excel for something else.

  • JGIT Profile Picture
    20 on at

    Thanks - that makes sense! Will try it and advise. 

     

  • Daniel_Pa Profile Picture
    244 on at

    I'm guessing that the Close Excel action follows other Excel actions? Try inserting a Wait before closing.

    I've had similar issues, where PAD seems to try to close the file, while still reading from it or something. This results in the process locking up - not failing, but not continuing either. Resulting in a complete halt of the entire robot.

    Adding Waits before the Close Excel fixed it.

    Seems to be a problem with shared files (we sync all local files with OneDrive).

  • JGIT Profile Picture
    20 on at

    Terminating gthe process excel.exe has worked as one solution. I think excel is staying open after the close command as it is checking to see if its in sync with sharepoint/onedrive. Ive tried a wait before close and a wait after close but it seems to need a terminate process command to really close. 

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    The excel.exe process does end properly after some time. Also, usually takes a little longer if you save when closing, rather than explicitly saving changes via the Save Excel action.

    Terminating the process should not be considered as a 'Solution', because that is in general one of the worst ways of going about it. It is the app that is not closed right away, not the document. You can verify that by trying to launch it again right after closing and you will see that there are no issues with the document being open.

    So, by terminating the process, you're basically braking the normal flow of Excel as an application, which is never a good thing. You should only do that in cases where not doing it prevents your flow from operating properly. 

  • JGIT Profile Picture
    20 on at

    Thanks - I have explicitly saved the excel instance/file. I have then set wait periods then set excel to close the instance and have then set further wait periods before attempting to continue with the flow/starting another flow that requires excel. In every case I end up with flow fails and multiple excel processes remaining open. 

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    You should also investigate the root cause behind the failure. Perhaps it fails before it actually closes the Excel instance?

    Also, you might as well continue with the flow even if the Excel process is running. As noted, some Excel processes continue running after closing the document. This simply means the app itself has not stopped yet, but the document is closed, meaning it should not affect any further actions in your flow whatsoever. You can thus continue, regardless of whether the process is running or not.

     

    You can also check if the specific Excel instance is alive or not by checking the handle on the instance variable.

    If %ExcelInstance.Handle% is not empty (can only be empty before opening Excel) and is not equal to 0, the Excel instance is alive. If it is equal to 0, the instance has successfully been closed, meaning the document is no longer open.

     

    We usually do it as follows:

    IF the instance is alive, we give it up to 5 seconds for the process to stop.

    If it then does not stop, we run a PowerShell script to close the Excel app:

    $excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
    $excel.Visible = $false
    $excel.DisplayAlerts = $false
    
    $excel.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
    Remove-Variable -Name Excel 
    [GC]::Collect() 
    [GC]::WaitForPendingFinalizers()
    if ($excel.Workbooks.Count -eq 0)
    {
     Stop-Process -Name EXCEL -ErrorAction SilentlyContinue
    }

     If this still does not close all Excel processes, then we terminate it.

  • JGIT Profile Picture
    20 on at

    Thanks again. Very useful. The flow continues and finishes fine. It's the next flow that fails as it is unable to open an excel file. The excel process is open and alive and effectively blocking opening another excel file (which happens to have the same name as the web backend always calls the downloads the same name - for the avoidance of doubt, i have deleted the previous file and even tried adding in a renaming step so that the subsquent flow can succeed - which it does but I then end up with multiple excel processes remaining live and the problem is exacerbated).

     

    Closing excel works fine when the excel files are locally saved and not synced elsewhere. It only occurs with excel files that are autosaved to sharepoint. 

  • Verified answer
    Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    @JGIT , this is a completely different issue. It's not closing the Excel that fails, its the Sync with OneDrive or SharePoint that makes your Excel flows fail. This happens a lot when writing (updating content) to Excel on such auto-synced network locations. We generally avoid doing that and if it ever is relevant, we copy the files locally, do any required adjustments and then push it to OD/SP afterwards.

     

    Reading such files is more or less okay. But as soon as any updates are involved, it tends to crash because of the auto-sync happening in the background. The only viable workaround for this is changing your SP settings to disable auto-sync and then manually pushing the files in.

     

    If you terminate the Excel process, you will actually end up with your files not getting synced properly, meaning that the next flow will use outdated files in SP.

     

    The other option is to do it via PA Cloud flows, which have direct integrations with files on SP, meaning you can read/write files from SharePoint in a safer way.

  • Community Power Platform Member Profile Picture
    on at

    Ashleypettit1_0-1659576399473.png

    Ashleypettit1_1-1659576437975.png

    Importantly this works most of the time yet not all the time. Thus I'm going to use the powershell script someone provided above. 

     

    I also use OneDrive so perhaps this is related. That said what I'm doing is running terminate excel and yet under task manager -> details I can see excel is still running. Perhaps this is the sync to Onedrive discussed above. 

     

    The reason this is annoying for me is I run rather long/complex flows. These flows occasionally run into random errors and if they do and the error is not locally handled then it restarts the flow. If excel remains open from the failed run then the subsent run will error saying the file is already open somewhere else.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 501 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 323 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard