Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am generating an excel file in a particular path

the path is concatenated with the file name :

Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\RevLog_reports\ABC"

so the default file name is "ABC."extension""

Here is the code to generate the file:

xlWorkBook = oAPP.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")

For nrow As Integer = 1 To newgrid.Rows - 2
For crow As Integer = 1 To newgrid.Cols - 2
xlWorkSheet.Cells(nrow, crow) = _
newgrid.Cell(nrow, crow).Text
Next
Next

xlWorkBook.SaveAs(filename)



I want the default file to be ABC(0).Xlsx
if second time file is generated it should be ABC(1).Xlsx
if a file is already existing with same name it should be incremented by +1 to avoid duplication.
like this each time file should be created without ambiguity in the folder.
As I am in a support project I cannot change the default path.
and I have to open the file as a popup while exporting the excel.
Posted
Updated 2-Jun-15 20:38pm
v2

1 solution

There are 2 Possibilities :

- you generate your Filename and after that you look in your path if this file exists. If not, you can use the Filename - else you have to increment (or add) your index and repeat this.

- you read the complete Directory of your path and look, which the last filename that matches is.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900