Option Explicit
Sub VerSaveAs( _
Optional SaveType As String = "????")
Dim Ans As VbMsgBoxResult
Dim ProcTitle As String
Dim SaveType2 As String
Dim strFileName As String
Dim strNewName As String
Dim strOldDate As String
Dim strOldName As String
Dim strSuffix As String
Dim strVerNum As String
Dim ThisPath As String
Dim localVerNum As Integer
ProcTitle = "VerSaveAs"
Step1:
Select Case LCase(SaveType)
Case "date", "vers", "????"
Case Else
MsgBox "invalid SaveType. File not saved.", vbCritical, ProcTitle
Exit Sub
End Select
Step2:
Select Case LCase(Application.Name)
Case "microsoft access"
Case "microsoft excel"
strFileName = ActiveWorkbook.Name
ThisPath = ActiveWorkbook.Path
Case "microsoft outlook"
Case "microsoft powerpoint"
strFileName = ActivePresentation.Name
ThisPath = ActivePresentation.Path
Case "microsoft project"
Case "microsoft visio"
Case "microsoft word"
strFileName = ActiveDocument.Name
ThisPath = ActiveDocument.Path
Case Else
MsgBox "unknown application; appl name = " & Application.Name & vbCrLf & _
"save NOT performed", vbCritical, ProcTitle
Exit Sub
End Select
strOldName = Left(strFileName, Len(strFileName) - 4)
strSuffix = Right(strFileName, 3)
Step3:
Select Case LCase(SaveType)
Case "date", "vers"
SaveType2 = SaveType
Case "????"
If strOldName Like "*;v###" Then
SaveType2 = "vers"
GoTo Step4
End If
If strOldName Like "*;####-##-##_######" Then
SaveType2 = "date"
GoTo Step4
End If
SaveType2 = "vers"
End Select
Step4:
Select Case LCase(SaveType2)
Case "date"
strOldDate = Right(strOldName, 17)
If strOldDate Like "####-##-##_######" Then
strOldName = Left(strOldName, Len(strOldName) - 18)
End If
strNewName = ThisPath & "\" & _
strOldName & ";" & Format(Date, "yyyy-mm-dd") & _
"_" & Format(Now, "hhmmss") & "." & strSuffix
Case "vers"
If strOldName Like "*;v###" Then
strVerNum = VerNum(Right(strOldName, 3), 3, 1)
localVerNum = strVerNum
If localVerNum > 999 Then
MsgBox "Next version number > 999; can not be accomodated" & _
vbCrLf & vbCrLf & _
"Current file will be saved with current version number." & vbCrLf & _
"You can change filename when system prompts to replace" & vbCrLf & _
"existing file", vbCritical, ProcTitle
strNewName = ThisPath & "\" & strOldName & "." & strSuffix
GoTo Step5:
End If
strNewName = ThisPath & "\" & Left(strOldName, Len(strOldName) - 3) & _
strVerNum & "." & strSuffix
Else
strNewName = ThisPath & "\" & strOldName & _
";v001" & "." & strSuffix
End If
End Select
Step5:
Select Case LCase(Application.Name)
Case "microsoft access"
Case "microsoft excel"
ActiveWorkbook.SaveAs FileName:=strNewName
Case "microsoft outlook"
Case "microsoft powerpoint"
ActivePresentation.SaveAs FileName:=strNewName
Case "microsoft project"
Case "microsoft visio"
Case "microsoft word"
ActiveDocument.SaveAs FileName:=strNewName
End Select
End Sub
Function VerNum(strOldVerNum, Length, Inc) As String
Dim NumZeros As Integer
Dim localVerNum As Long
localVerNum = CLng(strOldVerNum) + Inc
NumZeros = Length - Len(Trim(Str(localVerNum)))
If NumZeros < 0 Then
VerNum = "XXX"
Exit Function
End If
VerNum = String(NumZeros, "0") & Trim(Str(localVerNum))
End Function
Sub SaveAs_Test()
Dim SaveType As String
SaveType = InputBox("date or vers or ???? approach?")
If SaveType = "" Then Exit Sub
Call VerSaveAs(SaveType)
End Sub
|