I have got this to work, with a lot of trial and error, for the minor update at least.
The major update adds a whole digit rather than rounds up to the next number so you get V2.2 rather than V2.0.
Does anyone know how to fix that or have any other suggestions?
Code:
Sub SaveVersionedDocument()
Dim doc As Document
Dim fileName As String
Dim docName As String
Dim version As String
Dim currentDate As String
Dim folderPath As String
Dim minorUpdate As Boolean
' Set your document object (assuming you're running this from within Word)
Set doc = ActiveDocument
docName = Left(doc.Name, Len(doc.Name) - 23) ' Remove ".docx"
' Check if the document has been saved
If doc.Saved Then
' Extract existing file name, version, and date
fileName = Left(doc.Name, Len(doc.Name) - 16) ' Remove ".docx"
version = Mid(fileName, InStrRev(fileName, "V") + 1)
currentDate = Format(Date, "dd.mm.yy")
' Determine if it's a minor update
minorUpdate = InputBox("Is this a minor update? (Yes/No)") = "Yes"
'Update version and date
If minorUpdate Then
version = "V" & Format(CDbl(version) + 0.1, "0.0")
Else
version = "V" & Format(CDbl(version) + 1, "0.0")
End If
Else
' New document: prompt for file name
fileName = InputBox("Enter a file name (without extension):")
version = "V1.0"
currentDate = Format(Date, "dd.mm.yy")
End If
' Prompt user to select a folder
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
folderPath = .SelectedItems(1)
Else
MsgBox "Folder selection canceled. Document not saved."
Exit Sub
End If
End With
' Debug print the docName
Debug.Print "docName at end: " & docName
' Save the document
doc.SaveAs2 fileName:=folderPath & "\" & docName & " - " & version & " - " & currentDate & ".docx"
MsgBox "Document saved successfully!"
End Sub