'This kb article:
'http://www.vbaexpress.com/kb/getarticle.php?kb_id=1041
'Another sequential filename
'tstav,http://vbaexpress.com/kb/getarticle.php?kb_id=1008
Const Max_Path As String = 260
'http://msdn.microsoft.com/en-us/library/bb776479.aspx
Public Declare Function PathYetAnotherMakeUniqueName _
Lib "shell32.dll" _
( _
ByVal pszUniqueName As String, _
ByVal pszPath As String, _
ByVal pszShort As String, _
ByVal pszFileSpec As String _
) As Boolean
'http://msdn.microsoft.com/en-us/library/bb776479.aspx
Public Declare Function PathMakeUniqueName _
Lib "shell32.dll" _
( _
ByVal pszUniqueName As String, _
ByVal cchMax As Long, _
ByVal pszTemplate As String, _
ByVal pszLongPlate As String, _
ByVal pszDir As String _
) As Boolean
Function fMakeAnotherUnique(vShortTemplate, vLongTemplate, vFolder) As String
'vFolder can end in trailing backslash or not
Dim rc As Boolean, vUniqueName As String, s As String
vUniqueName = Space$(Max_Path)
rc = PathYetAnotherMakeUniqueName(vUniqueName, StrConv(vFolder, vbUnicode), _
StrConv(vShortTemplate, vbUnicode), StrConv(vLongTemplate, vbUnicode))
If rc Then
vUniqueName = StrConv(vUniqueName, vbFromUnicode)
fMakeAnotherUnique = vUniqueName
End If
End Function
Function MakeAnotherUnique(filespec As String) As String
MakeAnotherUnique = fMakeAnotherUnique("", GetFileName(filespec), GetFolderName(filespec))
End Function
Function fMakeUnique(vShortTemplate, vLongTemplate, vFolder) As String
'vFolder can end in trailing backslash or not
Dim rc As Boolean, vUniqueName As String, s As String
vUniqueName = Space$(Max_Path)
rc = PathMakeUniqueName(vUniqueName, Max_Path, StrConv(vShortTemplate, vbUnicode), _
StrConv(vLongTemplate, vbUnicode), StrConv(vFolder, vbUnicode))
If rc Then
vUniqueName = StrConv(vUniqueName, vbFromUnicode)
fMakeUnique = vUniqueName
End If
End Function
Function MakeUnique(filespec As String) As String
MakeUnique = fMakeUnique("", GetFileName(filespec), GetFolderName(filespec))
End Function
Function GetFileName(filespec As String) As String
Dim p1 As Integer, p2 As Integer
p1 = InStrRev(filespec, "\")
p2 = Len(filespec) - p1
GetFileName = Mid$(filespec, p1 + 1, p2)
End Function
Function GetFolderName(filespec As String) As String
Dim p1 As Integer
p1 = InStrRev(filespec, "\")
GetFolderName = Left$(filespec, p1)
End Function