Sub Test1()
Dim s As String
s = fMakeAnotherUnique("", Environ("username") & ".xls", ThisWorkbook.Path)
MsgBox s, vbInformation, "MsgBox1: fMakeAnotherUnique()"
s = fMakeAnotherUnique("", ThisWorkbook.Name, ThisWorkbook.Path)
MsgBox s, vbInformation, "MsgBox2: fMakeAnotherUnique()"
End Sub
Sub Test2()
Dim s As String
s = MakeAnotherUnique(ThisWorkbook.Path & "\" & Environ("username") & ".xls")
MsgBox s, vbInformation, "MsgBox3: MakeAntoherUnique()"
s = MakeAnotherUnique(ThisWorkbook.FullName)
MsgBox s, vbInformation, "MsgBox4: MakeAntoherUnique()"
End Sub
Sub Test3()
Dim s As String
s = fMakeUnique("", Environ("username") & ".xls", ThisWorkbook.Path)
MsgBox s, vbInformation, "MsgBox5: fMakeUnique()"
s = fMakeUnique("", ThisWorkbook.Name, ThisWorkbook.Path)
MsgBox s, vbInformation, "MsgBox6: fMakeUnique()"
End Sub
Sub Test4()
Dim s As String
s = MakeUnique(ThisWorkbook.Path & "\" & Environ("username") & ".xls")
MsgBox s, vbInformation, "MsgBox7: MakeUnique()"
s = MakeUnique(ThisWorkbook.FullName)
MsgBox s, vbInformation, "MsgBox8: MakeUnique()"
End Sub
Const Max_Path As String = 260
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
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
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
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
|