Welp, you've done it. I've tweaked the code a bit (see below) to open a Save As dialog and to adjust the filename depending on whether there is a space between the version number and the open parenthesis.
Code:
Sub SaveNewVersion_Word()
'PURPOSE: Save file, if already exists add a new version indicator to filename
'Graham Mayor - https://www.gmayor.com - Last updated - 04 Apr 2021
Dim sPath As String
Dim sName As String, sNewName As String
Dim sExt As String
Dim sVer As String
Dim vVer As Variant
Dim sDate As String
Dim sInitials As String
Dim newFileName As String
sInitials = "DSR"
sPath = ActiveDocument.Path
If sPath = "" Then GoTo NotSavedYet
sPath = sPath & "\"
sDate = Format(Date, "MM.DD.YY") ' This would be replaced when I get my date format check to work.
sName = ActiveDocument.Name
sExt = Right(sName, Len(sName) - InStrRev(sName, ".") + 1)
sNewName = Trim(Split(sName, "(")(0))
sVer = sNewName
vVer = Split(sVer, Chr(32))
sVer = onlyDigits(CStr(vVer(UBound(vVer))))
sName = Replace(sNewName, sVer, Format(sVer + 1, "00"))
'MsgBox sName & " (" & sInitials & Chr(32) & sDate & ")" & sExt
' Below code should find dates formatted with and without periods, but does not work that way.
' If InStrRev(ActiveDocument.Name, ".*)") > 0 Then
' sDate = Format(Date, "MM.DD.YY")
' Else
' sDate = Format(Date, "MMDDYY")
' End If
If InStrRev(ActiveDocument.Name, " (") > 0 Then
newFileName = sName & " (" & sInitials & Chr(32) & sDate & ")"
Else
newFileName = sName & "(" & sInitials & Chr(32) & sDate & ")"
End If
With Application.Dialogs(wdDialogFileSaveAs)
.Name = newFileName
.Show
End With
lbl_Exit:
Exit Sub
'Error Handler
NotSavedYet:
MsgBox "This file has not been initially saved. " & _
"Cannot save a new version!", vbCritical, "Not Saved To Computer" '
GoTo lbl_Exit
End Sub
One thing I would like to change is to set it such that it will also test whether the date contains periods. I tried including an If/Then, but it does not work, again apparently because of my InStrRev problem. How do I get InStrRev to use wildcards like Find? I would think that this code should return 07 where the filename is "Master Agreement 07 (JPM 04.03.21)"
Code:
Mid(sName, InStrRev(sName, "[0-9] & [0-9]", 2))
By the same token, wouldn't this return ".21)"?
Code:
Mid(sName, InStrRev(sName, ".*)", 4))