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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to tell Excel if f...
Power Automate
Suggested Answer

How to tell Excel if file is open then use that incident only

(1) ShareShare
ReportReport
Posted on by 75
Hi Experts
 
 
I think I posted that in wrong Forum thus received no response.
 
My Query - 
 
I'm using the following flow to start excel and run my morning macro. It works fine. 
 
Yesterday I encountered a situation where I was working and date changed so I needed to run the macro. Being experimental brain, I used PA to do the same. 
 
This time, it opened a new incidence of the same file, there by one incidence became read only and other it worked with. With two incidents of same file, it there by gave an error in running the macro, as it needed to save the file too.
 
So, is there any way to tell excel, if the file is open, use that only than opening all over again.
 
The flow I'm using is as below.
 
Thanks in advance.
Much appreciated.
 
Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''C:\\Users\\Sanja\\Dropbox\\All Records.xlsb''' Visible: True ReadOnly: False Instance=> R01
Excel.RunMacro Instance: R01 Macro: $'''\'All Records.xlsb\'!BanksSortMorning'''
  • Suggested answer
    Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at
     
    I have checked and could find that there is a possibility of powershell script to check whether excel process is running or not. But can we make that to check whether excel file is opened if yes- close the existing file and open it again - because attaching to excel may result in issues as there are some limitations i believe 
     
     
    This is the powershell script to check excel with given file name is opened
     
    $processes = Get-Process excel -ErrorAction SilentlyContinue
    
    # Check if any Excel process has the exact file name open
    $isRunning = "No"
    foreach ($process in $processes) {
        if ($process.MainWindowTitle -eq "%FileName% - Excel") {
            $isRunning = "Yes"
            break
        }
    }
    
    # Return the result
    $isRunning
     
    %FileName% is a variable passed from Power Automate into script
     
     

    Thanks & Regards,
    Nived N

    Stay connected:
    LinkedIn | YouTube | Blogs

    Was this answer helpful?
    If yes, please mark it as the solution by selecting the checkbox in the discussion thread.
    Your feedback motivates me to keep contributing. Thank you!

  • SanjayGMusafir Profile Picture
    75 on at
    @Nived_Nambiar Thanks for replying.
     
    Your idea is so correct but I'm unable to execute it. Actually not finding the right way to close excel when file is already open, though your script returns right message as Yes/No
     
    Here is my complete flow if you may help me further.
     
    Thanks a lot 🙏
     
    My flow - 
     
    File.GetPathPart File: $'''C:\\Users\\Sanja\\Dropbox/All Records''' FileName=> FileName
    @@copilotGeneratedAction: 'False'
    Scripting.RunPowershellScript.RunPowershellScript Script: $'''$processes = Get-Process excel -ErrorAction SilentlyContinue
    # Check if any Excel process has the exact file name open
    $isRunning = \"No\"
    foreach ($process in $processes) {
        if ($process.MainWindowTitle -eq \"%FileName% - Excel\") {
            $isRunning = \"Yes\"
            break
        }
    }
    # Return the result
    $isRunning''' ScriptOutput=> PowershellOutput
    IF PowershellOutput = $'''Yes''' THEN
        Excel.LaunchExcel.LaunchUnderExistingProcess Visible: True Instance=> ExcelInstance
        Excel.CloseExcel.Close Instance: ExcelInstance
    END
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''C:\\Users\\Sanja\\Dropbox\\All Records.xlsb''' Visible: True ReadOnly: False Instance=> R01
    Excel.RunMacro Instance: R01 Macro: $'''\'All Records.xlsb\'!BanksSortMorning'''
     

     
  • Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at
    Hi,
     
    Another way - why don't you try to kill the excel process using terminate process action - would that help- place the action at the beginning of the flow - then no powershell script is required :)
     

    Thanks & Regards,
    Nived N

    Stay connected:
    LinkedIn | YouTube | Blogs

    Was this answer helpful?
    If yes, please mark it as the solution by selecting the checkbox in the discussion thread.
    Your feedback motivates me to keep contributing. Thank you!

  • SanjayGMusafir Profile Picture
    75 on at
    @Nived_Nambiar Thanks fir reverting. Kill process doesn't seem to work, as it asks for process to kill.
     
  • Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at
    Hi ,
     
    Just specify EXCEL as process name there
     
     

    Thanks & Regards,
    Nived N

    Stay connected:
    LinkedIn | YouTube | Blogs

    Was this answer helpful?
    If yes, please mark it as the solution by selecting the checkbox in the discussion thread.
    Your feedback motivates me to keep contributing. Thank you!

  • SanjayGMusafir Profile Picture
    75 on at
    @Nived_Nambiar Thanks for reverting
     
    No such option there.
     
  • SanjayGMusafir Profile Picture
    75 on at
    @Nived_Nambiar Thanks a lot 🙏.
     
    It worked.
  • Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at
    Hi sanjay 
     
    Kindly mark the answer as solution so that we can close the topic
     

    Thanks & Regards,
    Nived N

    Stay connected:
    LinkedIn | YouTube | Blogs

    Was this answer helpful?
    If yes, please mark it as the solution by selecting the checkbox in the discussion thread.
    Your feedback motivates me to keep contributing. Thank you!

  • SanjayGMusafir Profile Picture
    75 on at
    @Nived_Nambiar I tried multiple times. But I believe there is some glitch. Hope it works this time.
     
    Thanks again
  • Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at
     
    did you meant by marking answer ?
     
     

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
11manish Profile Picture

11manish 237 Super User 2026 Season 2

#2
David_MA Profile Picture

David_MA 227 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 194

Last 30 days Overall leaderboard