Incremented File Name

How to save a file with an incremented filename if the file already exists

Public Sub SaveIncremental 
Dim sFolderPath As String
Dim icount As Integer

   sFolderPath = "C:\Temp\"
   If Dir(sFolderPath & "FileName" & ".txt") = "" Then
'save the file, since it does not currently exist
   Else
      icount = 1
      Do While Dir(sFolderPath & "FileName" & icount & ".txt") <> ""
         icount = icount + 1
      Loop
'save the file as sFolderPath & "FileName" & icount & ".txt"
   End If
End Sub



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrev