Creating Excel File with Dynamic Names

uiautomation
file
studio

#1

Hi,

Is it possible to create a filepath dynamically for Excel?

I am struggling to do it currently

So I want to do something similar to below

ExcelPath = “C:\Path”+Variable+".xlsx"

Thanks in advance
R


#2

Hi.

Here is something I do from time to time, is replace a word in the string.
For example,

filepath = "c:\path\filename yyyy-MM.xslx"
filepath.Replace("MM",Now.ToString("MM")).Replace("yyyy",Now.ToString("yyyy"))

or

filepath = "C:\path\filename %date%.xlsx"
filepath.Replace("%date%",Now.ToString("yyyy-MM"))

or alternatively just simply concatenate variables together like you show. ExcelPath = filepath.Replace(".xlsx","")+variable+".xlsx"

You can also look at a few methods online using Path.GetFilename(), Path.GetExtension(), or Path.GetDirectoryName() and then manipulate the filepath.

Regards.


#3

I’d recommend using String.Format for these:

filepath = String.Format("c:\path\filename {0}.xslx", Now.ToString("yyyy-MM"))
or
filepath = String.Format("c:\path\filename {0}-{1}.xslx", Now.ToString("yyyy"), Now.ToString("MM"))

Btw - did anyone make interpolated strings work in UiStudio? Syntax doesn’t seem to be correct for some reason, even though it should work.


#4

@andrzej.kniola Good idea.
The only thing I have against that method is that you can’t really tell what the {0} is identifying in the string, like what does it represent and what are you putting in its place? I do like that method though and will definitely consider it in the near future.

Thanks.


#5

Thanks all for help. That worked


#6

HI All,

I am trying to do the same thing to create dynamic excel file names, but the excel file is not created. Could you please advice if I am missing something.

Capturefile_name=String.Format(“C:\Users\UiPath\demo {0}.xslx”, Now.ToString(“yyyy-MM”))


#7

Newest Excel pack doesn’t create one by default:

Specifically:

To avoid situations when a new workbook was automatically created when the path was not correctly specified, Excel Application Scope now has a new property, Create New File. Once selected, if no workbook can be found at the specified path, a new workbook file is created at the same path.


#8

Hi @RogerFenergo,

I’ve made an example for you to try and understand the dynamic creation of excel files.

excel dyanmic.xaml (14.2 KB)

Just be sure to changes the values of “path” to a folder called “tester” on your desktop. Copy its path and assign that to the path variable.

C:/*/Desktop/tester

and don’t include a slash after the last folder as that would cause an error.

Try and see if you can understand the program, and if you face any issues, or if you need an explanation, please feel free to let me know and I’ll try and work it out for you.

All the Best!

Regards,
Abdullah

EDIT 1: Do add an activity to say, write cell A1, before you execute the bot. I seem to have forgotten to add it. My apologies.