Safely Close Excel Workbook

Hi All,

I am downloading data from an application and it automatically opens it in an excel file when I download it.
I then find the most recent file name within the folder (it has a different name each time it is downloaded)

Is there a way I can use the workbook name I have found and stored to attach to the already open Excel file and read the data?

At the moment I am having the ‘KILL’ Excel to close the excel workbook and then use the ‘Read Range Workbook’ activity to pull in the data. There doesn’t appear to be a close workbook activity anymore???

Close Workbook should still be available, make sure to filter for “Classic”:
image

Thanks
How do I generate a WorkbookApplication variable for the workbook name?
I currently have it stored as a string.

This is how I am determining the filename at the moment.

Hi,

what you’re trying to achieve doesn’t seem to exist out-of-the-box unfortunately, so I think you have two options:

Instead of Directory.GetFiles you can use the Wait for Download activity and it’ll give you a FileInfo variable with the file’s info. yourFileInfo.Name is the name of the file, yourFileInfo.FullName is the entire path and filename.

He already has the name of the file as a string (including the path). He was asking how to create a WorkbookApplication object from that full path so that he can use it as an input for the Close Workbook classic activity.

You get that from the variable in “Reference as” in the Use Excel File activity. Well darn, that gives you a WorkBookQuickHandle not a WorkBookApplication. Odd. I’ll research.

Not in his case. Please read the full thread and his original post:

I am downloading data from an application and it automatically opens it in an excel file when I download it.
I then find the most recent file name within the folder (it has a different name each time it is downloaded)

Is there a way I can use the workbook name I have found and stored to attach to the already open Excel file and read the data?

The Excel file is already opened (not as part of the automation). He wants to see how/if he can attach the Use Excel File to an open file (which doesn’t seem to be possible) or use Close Workbook to close that Workbook so that he can open it again (through the automation).

Oh in that case just use Use Application/Browser to attach to the Excel window and set the Close property to always. No activities inside the Use Application/Browser. It’ll close it.

1 Like

Have you tried using the “modern” excel activities? In Excel Process Scope you can define exactly that within the “Process mode” properties:

  • Reuse if exists - Searches for any open Excel processes when the first Use Excel File activity is executed to verify if a file is already open. If an open file is found, that process is associated with Excel Process Scope. In the event that another open file is found, the File conflict resolution setting is applied.
  • Only if exists - Searches for any open Excel processes when the first Use Excel File activity is executed to verify if a file is already open. If an open file is found, that process is associated with Excel Process Scope. If no open files are found, an exception is thrown.

Source:
https://docs.uipath.com/activities/other/latest/productivity/excel-process-scope-x

Good point, but I tried to use it myself (selecting “Reuse if exists”) and I had a hard time making this work if the file you’re trying to use within the Excel scope (with the Use Excel File activity) is also the same file that is already open in Excel (and that was opened outside of the context of the automation. I was getting an error message and I figured that this scenario wasn’t supported. Do you happen to know if this can work?

1 Like

Is there a risk to the Classic activities that the activity will eventually be discontinued and we will need to find another solution?

Still stuck on creating a Workbook Application variable I’m afraid