You need a Word macro that saves your document automatically with a filename that follows a specific pattern such as ProjectName_Date_Version.docx. The built-in AutoRecover feature in Word does not allow custom filenames and saves only to a fixed location. This article explains how to write a VBA macro that creates a custom filename from document content, the current date, and a version number. You will learn the code structure, how to install it, and how to trigger it on save or on a timer.
Key Takeaways: Building a Custom Auto-Save Macro in Word VBA
- Alt+F11 (VBA Editor): Open the editor to insert and edit the macro code.
- File > Options > Customize Ribbon > Developer: Enable the Developer tab to access macro tools.
- Document.BuiltInDocumentProperties: Use this object to pull the title or subject from document properties into the filename.
- Format(Date, “yyyymmdd”): Generate a date string in a consistent format for the filename.
- Application.OnTime: Schedule the macro to run on a repeating timer for automatic saves.
How the Auto-Save Macro Works and What You Need
The macro uses Word’s VBA (Visual Basic for Applications) to construct a filename from dynamic parts and then save the document to a chosen folder. The core logic reads the current document’s built-in properties such as Title or Subject, adds the current date using the Format function, and optionally increments a version number stored in a custom document property or a separate text file.
Before writing the macro, you must enable the Developer tab in Word. Go to File > Options > Customize Ribbon, check the Developer box in the right pane, and click OK. This tab gives you access to the VBA editor, macro recording, and form controls.
Prerequisites for the Macro
The macro relies on the following elements:
- Document Properties: Set a Title or Subject in File > Info > Properties > Advanced Properties. The macro reads these to build the filename.
- A Save Folder: Choose a folder path where the macro will save files. Use a fixed path like “C:\MyAutoSaves” or a network share.
- Macro Security: Set macro security to allow signed or trusted macros. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings, then select “Enable all macros” only for testing.
Steps to Write and Install the Auto-Save Macro
The following steps create a macro that builds a filename from the document title, the current date, and a version number. The macro saves the document to a folder you specify.
- Open the VBA Editor
Press Alt+F11 to open the VBA editor. In the Project Explorer pane, expand Normal > Modules. Right-click Modules and choose Insert > Module. A blank code window appears. - Write the main auto-save subroutine
Paste the following code into the module window:Sub AutoSaveCustom()
Dim doc As Document
Set doc = ActiveDocumentDim baseName As String
baseName = doc.BuiltInDocumentProperties(wdPropertyTitle)
If baseName = "" Then baseName = "Untitled"Dim dateStr As String
dateStr = Format(Date, "yyyymmdd")Dim versionNum As Long
versionNum = GetNextVersionNumberDim folderPath As String
folderPath = "C:\MyAutoSaves\"Dim fullName As String
fullName = folderPath & baseName & "_" & dateStr & "_v" & versionNum & ".docx"Application.DisplayAlerts = False
doc.SaveAs2 FileName:=fullName, FileFormat:=wdFormatXMLDocument
Application.DisplayAlerts = True
End Sub - Add a helper function for version numbers
Below the subroutine, paste this function to read and increment a version counter stored in a custom document property:Function GetNextVersionNumber() As Long
Dim cp As DocumentProperty
On Error Resume Next
Set cp = ActiveDocument.CustomDocumentProperties("Version"))
If Err.Number <> 0 Then
ActiveDocument.CustomDocumentProperties.Add Name:="Version", _
LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=1
GetNextVersionNumber = 1
Else
GetNextVersionNumber = cp.Value + 1
cp.Value = GetNextVersionNumber
End If
On Error GoTo 0
End Function
Note: The custom property “Version” is created on first use. Each save increments it by one. - Run the macro manually to test
Close the VBA editor. Press Alt+F8, select AutoSaveCustom, and click Run. The document saves to the folder you specified. Check that the filename matches the pattern, for example “Report_20250321_v3.docx”. - Set the macro to run automatically on save
To trigger the macro each time you press Ctrl+S, rename the subroutine to Document_Save. However, a simpler method is to attach the macro to a keyboard shortcut. Go to File > Options > Customize Ribbon > Customize (next to Keyboard shortcuts). In the Categories list, select Macros. In the Commands list, select AutoSaveCustom. Press a new shortcut key such as Ctrl+Shift+S and click Assign. - Schedule the macro on a timer for automatic saves
Add this subroutine to the same module to run AutoSaveCustom every 5 minutes:Sub StartAutoSaveTimer()
Application.OnTime When:=Now + TimeValue("00:05:00"), _
Name:="AutoSaveCustom"
End Sub
Run StartAutoSaveTimer once. The macro runs after 5 minutes and then stops. To make it repeat, add a call to StartAutoSaveTimer at the end of AutoSaveCustom.
Common Errors and Adjustments
Macro does not run because of security settings
If the macro does not run, check macro security. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select “Enable all macros” for testing. For production, digitally sign the macro or store it in a trusted location.
Filename contains invalid characters from document title
Document titles may include characters that Windows does not allow in filenames, such as / : ? ” < > |. Add a sanitization function that replaces these characters with an underscore. Insert this function in the module and call it before building the filename:
Function SanitizeFilename(ByVal text As String) As String
Dim illegalChars As String
illegalChars = "\/:?""<>|"
Dim i As Integer
For i = 1 To Len(illegalChars)
text = Replace(text, Mid(illegalChars, i, 1), "_")
Next i
SanitizeFilename = text
End Function
Version number resets after closing the document
The custom document property “Version” is stored inside the document. When you close and reopen the document, the property persists. If you start a new document, the version counter begins at 1. To maintain a global version across documents, store the counter in a text file on disk and read it with VBA’s FileSystemObject.
VBA Auto-Save vs Built-In AutoRecover
| Item | VBA Auto-Save Macro | Built-In AutoRecover |
|---|---|---|
| Filename control | Full custom pattern | Fixed names (Backup of…) |
| Save location | Any folder you specify | Fixed AutoRecover folder |
| Version numbering | Automatic increment | Not available |
| Schedule | Timer or manual shortcut | Fixed interval (1-120 minutes) |
| Recovery on crash | Manual reopen of last save | Automatic prompt on restart |
The VBA macro gives you full control over the filename pattern and save location. The built-in AutoRecover is simpler and recovers unsaved work after a crash. For maximum safety, use both: keep AutoRecover enabled for crash recovery and run the VBA macro for versioned backups.
You can now build a Word VBA macro that saves your document with a custom filename pattern using the document title, date, and version number. Start by testing the macro on a sample document with a simple folder path. After verifying the output, attach the macro to a keyboard shortcut or a timer to automate your backup workflow. For advanced scenarios, extend the macro to save to a network drive or add a timestamp to the filename.