Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-28-2024, 10:00 PM
hh vba hh vba is offline Save word document with a new version number using VBA Windows 11 Save word document with a new version number using VBA Office 2021
Novice
Save word document with a new version number using VBA
 
Join Date: Aug 2024
Posts: 4
hh vba is on a distinguished road
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I recover a word document that was 'Saved-over' with another (old) version of same document? Mrsolace Word 2 09-10-2021 10:50 AM
Save document as new version with specific format tanko Word VBA 4 04-05-2021 08:06 PM
Save word document with a new version number using VBA Version number from document filename via field code palanski Word 3 10-15-2014 01:54 PM
Version number for Word document, automated by VCS lblythen Word 4 04-08-2014 11:24 PM
Save word document with a new version number using VBA Word fails to save backup of previous version of saved document musawwir Word 1 11-05-2012 05:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft