Here is the function for creating unique filepath (taken from http://mielk.pl/):
Public Function uniqueFilePath(filepath As String) As String
Const METHOD_NAME As String = "uniqueFilePath"
'------------------------------------------------------------------------------------------------------
Static objFSO As Object '(Late binding that allows to use the function, even if
'Microsoft Scripting Runtime library is not loaded)
Dim strFileExtension As String
Dim strFileName As String
Dim strParentFolder As String
Dim strTempFilePath As String
Dim intCounter As Integer
'------------------------------------------------------------------------------------------------------
'Create FileSystemObject instance if it hasn't been created yet. ------------------------------------|
If objFSO Is Nothing Then '|
Set objFSO = VBA.CreateObject("Scripting.FileSystemObject") '|
End If '|
'----------------------------------------------------------------------------------------------------|
With objFSO
'Checks if the file already exists. -------------------------------------------------------------|
If .fileExists(filepath) Then '|
'|
'If the given filepath already exists, function transforms its name by '|
'appending the number in brackets. '|
strParentFolder = .GetParentFolderName(filepath) '|
If Not VBA.right$(strParentFolder, 1) = "\" Then strParentFolder = strParentFolder & "\" '|
strFileName = .GetBaseName(filepath) '|
strFileExtension = "." & .GetExtensionName(filepath) '|
'|
'------------------------------------------------------------------------------------| '|
Do '| '|
intCounter = intCounter + 1 '| '|
strTempFilePath = strParentFolder & strFileName & _
" (" & intCounter & ")" & strFileExtension '| '|
Loop While .fileExists(strTempFilePath) '| '|
'------------------------------------------------------------------------------------| '|
'|
uniqueFilePath = strTempFilePath '|
'|
Else '|
'|
'Specified filepath is unique in the file system and is returned in its original form. '|
uniqueFilePath = filepath '|
'|
End If '|
'-------- [If .FileExists(filepath) Then] -------------------------------------------------------|
End With
End Function
In order to make the code below work properly you must paste it in your code.
If the filepath you give as a parameter already exists, function returns the same filepath with the number in bracket appended, i.e. if file "C:\file.xlsx" already exists function returns "C:\file (1).xlsx".
If this file doesn't exists the function returns the original filepath without any changes.
Replace all the code you pasted in your question with the below:
Dim filepath As String
filepath = uniqueFilePath(FILE_PATH & personList(i, 1) & FILE_EXT)
Call .SaveAs(filepath)
Call .Close
.SaveAs2
isn't available in Excel VBA - are you using this in another program? Or are you working with word documents? – SierraOscar Jul 29 '15 at 15:25