How to check if any Excel file is open?

My program deals with lots of Excel files, and keeps opening and closing them during the course of the execution. Before the user can run this robot, I want to give them a warning saying they need to close all Excel files if there is an Excel file, or more than Excel files that are open.

But I am not sure how to make the robot detect an open Excel file. I don’t want this warning to be displayed if there is no Excel file that is currently open.

I am using the following logic, but this doesn’t always work.

  1. Get Processes Activity:
    – Output: allProcesses

  2. For Each Activity:
    For Each process in allProcesses
    —> if process.ToString.Contains(“Excel”) or process.ToString.Contains(“EXCEL”)
    ---------> Message Box Activity: “Please close Excel programs.”
    ---------> Break Activity:

This works when there is actually an open Excel workbook. But sometimes, even though there is no Excel file open, it still displays the message. I checked the task manager, and there was an Excel process even though no Excel file was actually open.

How can I modify this logic to make it work 100%?

Note: I don’t want to use “Kill Process”. I would rather warn the user and let them choose to close the application.

There were a couple of approaches to this. Like you could use a Workbook Read Range and Catch the error if it occurs, meaning the file is in use.

The approach I started using was to use a filestream Open, like this:

(EDIT: remove Retry Scope if desired as it was intended to “wait” until the file becomes available)
The above is if you are only targetting specific files, not applications.

If you want to lock the file into Read-only while the robot uses it, you can also add an Assign below this:
image

but, make sure it is set back to False when finished.

For multi-threading (meaning multiple Robots trying to use the same file), this will become more challenging to get right.

Make sure you are using the Close Workbook activity at the end of Excel Scopes. I have seen sometimes where the file is left open in the background leaving the process there. Also, if Preview Pane is enabled in File Explorer, this will cause Excel to show up as a process from being previewed.

Regards.

1 Like

Hi @tomato25

Use Excel application scope and use workbook variable
For each item.wb.get sheets and use read range and do the manipulations and in the last use close workbook

Thanks
Ashwin.S

Thanks! What is outputLogFilepath though?
I actually cannot guess what kind of Excel files the user will be using, so I cannot specify any file paths. Even though my program shouldn’t affect irrelevant Excel files that might be open at the time of execution, I still want the user to close ALL excel files/workbooks whether or not they will be affected by my program.

Also, regarding Close Workbook, I thought using Excel Application Scope will automatically close the workbook and its process when it exists the scope? Maybe it was the cause of it? Sometimes, I do see the Excel application running in the background… and I was wondering how it happened. I don’t wanna explain how to open task manager and delete the Excel process etc because they are not IT nor tech people, and I want them not to have to deal with anything too technical.

It’s a variable that represents the file path to the file that is being executed by the process, so it can look at it and determine if it is being used by someone.

Most of the time, your process will try to perform on a specific file, whether it is dynamic or a particular file path set from the start, the process will still need the file path at some point. So, I suppose you could make it end at the moment when it needs that file and it is opened, or even just pause the process until it is closed, waiting for a certain amount of time.

But, if you are looking at only the application, then you will have two options: GetProcesses (which sounds like might be more trouble than it’s worth if Excel is opened in the background) OR a Find Element / Element Exist.

So, I suggest trying to use the Find Element or Element Exist. This will allow you to see if there is an existing Excel window. Just wildcard the title attributes where the filename will be, so it sees all windows that are opened.

Regards.

It’s supposed to, but doesn’t always work… atleast from my observation. I have had issues with Excel automation prior to using Close Workbook activities.

Hmm, does using Kill Process activity kill the background excel processes as well?

Since I cannot use actual file paths to target specific files, I might be better off using Kill Process even though this was not my favorite activity.
Like using Dialog Box, ask the user if they are okay with closing all Excel files. If they choose no, stop the execution right there, but if they choose yes, just kill Excel processes and then proceed.

But if this doesn’t actually kill the background excel process, it won’t help…

Since you are looking for user interaction experience, I suggest trying my other suggestion to look for the element. You can use Element Exists to see if any Windows are opened, and this will only be true if the user can see the physical window. This is also how you would “soft” kill the processes by using this inside a Retry Scope which can loop and close all windows.

4 Likes

Sounds like the solution for my case. I appreciate your help, man!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.