Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-28-2024, 06:42 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 Save word document with a new version number using VBA

Hi everyone



I would like to create a macro using Word VBA to save a new version of the document I am working on.

If it is a new document, that hasn't yet been saved, I would like an input box to ask for a name for the file.

The document should then be saved with that file name and add a version number (e.g. V1.0) and the current date (in the format dd.mm.yyyy).

It should look like "Test - V1.0 - 01.01.24".

The user should be prompted to select a folder to save the document.

If the document already has been saved and named, I would like to be able to:

(a) save a minor version update with the version number increasing by 0.1 and to update the date e.g. "Test - V1.1 - 02.01.24"; and

(b) save a major version update with the version number increasing to the next whole number and to update the date e.g. "Test - V2.0 - 03.01.24".

The document should save into the existing folder in that format.

Does anyone know how to achieve that?

I've been trying all morning by searching forums and getting ideas from Copilot but haven't been able to figure it out.
Reply With Quote
  #2  
Old 08-28-2024, 08:30 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've got this, which works for creating the initial file, but comes up with a Run-time error '13': Type Mismatch on this line: version = "V" & Format(CDbl(version) + 0.1, "0.0")

Code:
Sub SaveVersionedDocument()
    Dim doc As Document
    Dim fileName 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
    
    ' 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) - 5) ' 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 = Replace(version, ".", "") ' Remove dots
            version = "V" & Format(CDbl(version) + 0.1, "0.0")
        Else
            version = "V" & Format(CDbl(version) + 1, "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
    
    ' Save the document
    doc.SaveAs2 fileName:=folderPath & "\" & fileName & " - " & version & " - " & currentDate & ".docx"
    
    MsgBox "Document saved successfully!"
End Sub
Reply With Quote
  #3  
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
  #4  
Old 08-28-2024, 10:52 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

Ok in case anyone is interested, I finally got this to work:

Code:
Sub SaveVersionedDocument3()
    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
      
   
    ' Check if the file name is longer than 16 characters (which means it has been saved before)
        pathlength = Len(doc.Name)
        Debug.Print "pathlength: " & pathlength
        'Update the fileName if it has been saved before
        If pathlength > 16 Then
        ' Extract existing file name, version, and date
        fileName = Left(doc.Name, Len(doc.Name) - 16) ' Remove version and ".docx"
        version = Mid(fileName, InStrRev(fileName, "V") + 1)
        currentDate = Format(Date, "dd.mm.yy")
        docName = Left(doc.Name, Len(doc.Name) - 23) ' Remove ".docx"
        folderPath = ActiveDocument.Path
      
        ' Determine if it's a minor update
        minorUpdate = InputBox("Is this a minor update? (Y/N") = "Y"
        
        'Update version and date
        If minorUpdate Then
            version = "V" & Format(CDbl(version) + 0.1, "0.0")
        Else
            version = "V" & Format(Ceiling(version), "0.0")
        End If
    Else
        ' New document: prompt for file name
        docName = InputBox("Enter a file name (without extension):")
        version = "V1.0"
        currentDate = Format(Date, "dd.mm.yy")
        ' 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
    End If
   
    ' Save the document
    doc.SaveAs2 fileName:=folderPath & "\" & docName & " - " & version & " - " & currentDate & ".docx"
    
    MsgBox "Document saved successfully!"
End Sub

Function Ceiling(ByVal num As Double) As Double
    If num = Int(num) Then
        Ceiling = num + 1
    Else
        Ceiling = Int(num) + 1
    End If
End Function
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 06:55 PM.


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