[FONT=Courier New][SIZE=1]Option Explicit[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Public Function NextFilename( _[/SIZE][/FONT]
[SIZE=1][FONT=Courier New] ByVal argFilename As String, _[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] ByVal argSuffixLength As Integer, _[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] ByVal argSeparator As String, _[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] ByVal argFolder As String) As String[/FONT][/SIZE]
[FONT=Courier New][SIZE=1]Dim iPtr As Integer[/SIZE][/FONT]
[SIZE=1][FONT=Courier New]Dim FileRoot As String[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]Dim FileExt As String[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]Dim sSuffix As String[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]Dim sFound As String[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]iPtr = InStrRev(argFilename, ".")[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]If iPtr = 0 Then[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] FileRoot = argFilename[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] FileExt = ""[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]Else[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] FileRoot = Left(argFilename, iPtr - 1)[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] FileExt = Mid(argFilename, iPtr)[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]End If[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]sSuffix = ""[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]For iPtr = Len(FileRoot) To 1 Step -1[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] If IsNumeric(Mid(FileRoot, iPtr, 1)) Then[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] sSuffix = Mid(FileRoot, iPtr, 1) & sSuffix[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] End If[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]Next iPtr[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]If Right(argFolder, 1) <> "\" Then argFolder = argFolder & "\"[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]sFound = Dir(argFolder & argFilename)[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]Do Until Len(sFound) = 0[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] If Len(sSuffix) = 0 Then[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] sSuffix = String(argSuffixLength - 1, "0") & "1" [/FONT][/SIZE][FONT=Courier New][SIZE=1][COLOR=green]' start numbering at "1" - change to "0" if required[/COLOR][/SIZE][/FONT]
[SIZE=1][FONT=Courier New] Else[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] sSuffix = CStr(CInt(sSuffix) + 1)[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] sSuffix = Right(String(argSuffixLength, "0") & sSuffix, argSuffixLength)[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] End If[/FONT][/SIZE]
[SIZE=1][FONT=Courier New] sFound = Dir(argFolder & FileRoot & argSeparator & sSuffix & FileExt)[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]Loop[/FONT][/SIZE]
[SIZE=1][FONT=Courier New]NextFilename = FileRoot & argSeparator & sSuffix & FileExt[/FONT][/SIZE]
[FONT=Courier New][SIZE=1]End Function[/SIZE][/FONT]