Can I open excel file based on current Month information?

Hello all,

Can I open excel file based on current Month information?
For example, open 2021-02 file when this month is Feb and open 2021-03 file when we’re on Mar. My code faced error message…Please refer to image.

Hi @Dorothy_lee … I see you have quotes for ‘yyyy’ inside tostring…try“yyyy-MM”)

Please refer this post


Use Directory.Get Files and In For each you can check that item.Contains(DateTimeNow.ToString(yyyy-MM)

If the remaining file name is static then you have use DateTimeNow.ToString(yyyy-MM) and remaining file name

Hope this may help you


It should be:

str_CurrentMonthlyReport = Directory.GetFiles(“yourpath”,“”“yyyy-MM”)+".xlsx")(0)

You can also create check for that (if for some reason there is no report for current date):

bool_ReportExist = not string.isNullorEmpty(str_CurrentMonthlyReport)

If you will get true that means report was found.

@Yameso Hello, Thank you for reply !
Found that below error message. Did I set something wrong?

str_CurrentReport is String.

Sorry my mistake:


I wrote it on my cell phone and it cut my * in text

But this error show sth in case there is no raport with current month. It should be sth like this:

This arr_Files is array of Strings
Value for it:


In case of missing report or two files with report you can for example throw exception, send email or anything else. If there is only one file you will get path to it in str_CurrentMonthReport :slight_smile:

@Yameso Thank you so much for helping.
By the way, I found that below error message but seems like there is no incorrect configuration. Could you please look at it?
Error : String cannot be converted to -1 dimensional array of string


Hi @Dorothy_lee -Could you please share , what you have in the Directory.getfiles code…?

Hello, here it is.

@Dorothy_lee - (0) means first file in the array…which means it will be assigned to only String…since Arr_Files is string array…you are getting an error…

Please remove the (0)…then the error will go away.

@prasath17 Thank you!! It’s perfectly working!!! :grinning: without (0)

1 Like

@Yameso Thank you ~!
I solve the issue with your suggestion and found that without (0) , it’s working totally fine.

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