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.
image

Hi @Dorothy_lee … I see you have quotes for ‘yyyy’ inside tostring…try

now.date.tostring(“yyyy-MM”)

Please refer this post

@Dorothy_lee

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

Thanks
Thanks

It should be:

str_CurrentMonthlyReport = Directory.GetFiles(“yourpath”,“”+today.date.ToString(“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?
image


str_CurrentReport is String.

Sorry my mistake:

Directory.GetFiles("yourpath","*"+Today.ToString("yyyy-MM")+"*.xlsx")(0)

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:

Directory.GetFiles("yourpath","*"+Today.ToString("yyyy-MM")+"*.xlsx")

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.
Directory.GetFiles(“D:\Users\01048398\Downloads\P00433_Cash_Report\02.Main_CashReport”,""+Today.ToString(“yyyy-MM”)+".xlsx")(0)

@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.