How to save a document using a filename that gets incremented by 1 each time if the filename already exists

Article contributed by Dave Rado


Sub SaveIncrementedFilename()

Dim PathAndFileName As String, n As Long

PathAndFileName = "C:\Data\temp"
If Dir(PathAndFileName & ".doc") = "" Then
    ActiveDocument.SaveAs (PathAndFileName & ".doc")
    n = 1
    Do While Dir(PathAndFileName & n & ".doc") <> ""
        n = n + 1
    ActiveDocument.SaveAs PathAndFileName & n & ".doc"
End If

End Sub

Click to view Terms of Use page

Click to view Disclaimer page