When you work with many Word documents, keeping file names consistent becomes a challenge. You may need to include dates, project codes, version numbers, or author initials in every file name. Manually typing these each time leads to errors and wasted time. This article explains how to use Word’s built-in fields and macros to automatically apply a specific file naming convention when you save a document. You will learn to set up a template that inserts dynamic content like the current date or a custom document property directly into the file name.
Key Takeaways: Automate File Naming in Word
- Quick Parts > Field > CreateDate or DocProperty: Insert dynamic content into your document that can be used as part of a file name.
- File > Save As with a custom AutoText entry: Build a reusable file name skeleton that includes static and dynamic parts.
- VBA macro tied to the BeforeSave event: Automatically rename and save the document following your convention without manual typing.
Understanding File Naming Convention Automation in Word
Word does not have a built-in feature that enforces a specific file naming convention at the moment you press Save. The Save As dialog shows the document title as the default file name, but you must edit it manually. To automate this, you need to combine several Word tools: fields for dynamic content, AutoText for reusable text blocks, and macros for full automation.
A file naming convention typically includes static parts like a project code and dynamic parts like the current date or a version number. For example: PRJ2024_2025-03-21_v2.docx. You can store the static parts in a custom document property and insert the dynamic parts using Word fields. Then, with a macro, you can assemble these pieces into the file name every time you save.
Prerequisites for Using Macros
Macros require that you enable the Developer tab in Word. Go to File > Options > Customize Ribbon and check the Developer box in the right pane. Also, macros use VBA code, so you must save your file as a macro-enabled document (.docm) or as a template (.dotm) if you want the macro available in all new documents.
Steps to Create a Reusable File Naming Convention
The following method uses a custom document property and a field to build a file name that you can copy into the Save As dialog. This approach works without macros and is safe for users who cannot run VBA code.
- Add a custom document property for the project code
Click File > Info > Properties > Advanced Properties. Go to the Custom tab. Type a name likeProjectCode. Set the Type to Text. Enter the value, for examplePRJ2024. Click Add, then OK. - Insert a field that combines static and dynamic content
Place the cursor where you want the file name skeleton to appear. Press Ctrl+F9 to insert a pair of field braces. Inside the braces, type:DocProperty ProjectCode \ MERGEFORMAT. Press F9 to update the field. Then type a separator like an underscore, and insert another field: press Ctrl+F9 and typeCreateDate \@ "yyyy-MM-dd" \ MERGEFORMAT. Press F9 again. Your field should look like:{ DocProperty ProjectCode \ MERGEFORMAT }_{ CreateDate \@ "yyyy-MM-dd" \ MERGEFORMAT }. - Save the field as an AutoText entry
Select the entire field result (the text that appears after updating). Press Alt+F3. In the Create New Building Block dialog, type a name likeFileNameSkeleton. Choose AutoText as the Gallery. Click OK. - Use the AutoText entry when saving
Press Ctrl+S to open the Save As dialog. Click in the File name box. Type a space, then press F3. The AutoText entryFileNameSkeletonappears. Press Enter to insert the skeleton, then edit the file name as needed and click Save.
Automating the File Name With a Macro
If you want the file name to be generated automatically without any manual typing, use a VBA macro. This macro runs every time you save the document. It reads the custom property and the current date, then sets the file name accordingly.
- Open the VBA editor
Press Alt+F11. In the Project Explorer, double-click ThisDocument. This opens the code window for the current document. - Paste the macro code
Copy the following code into the window:Private Sub Document_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim projectCode As String Dim dateStr As String Dim baseName As String Dim fullName As String Dim docPath As String Dim doc As Document Set doc = ActiveDocument ' Read custom property On Error Resume Next projectCode = doc.CustomDocumentProperties("ProjectCode") On Error GoTo 0 If projectCode = "" Then projectCode = "NOCODE" ' Format current date dateStr = Format(Date, "yyyy-mm-dd") ' Build file name baseName = projectCode & "_" & dateStr & "_v1" ' If the document has never been saved, set the file name If doc.Path = "" Then If SaveAsUI Then ' The Save As dialog will appear; we set the suggested name ' This requires a workaround: we cannot set the dialog field directly ' Instead, we store the name in a variable for later use End If Else ' If already saved, rename and save docPath = doc.Path & "\" & baseName & ".docm" doc.SaveAs2 FileName:=docPath Cancel = True ' Prevent default save End If End Sub - Save and test the macro
Close the VBA editor. Save the document as a macro-enabled document (.docm). Press Ctrl+S. The macro runs and saves the file with the naming convention based on the custom property and current date.
Common Issues When Automating File Naming
The Macro Does Not Run When I Press Save
Macros are disabled by default when you open a document from the internet or an email. Check the security settings: go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros. Also ensure the file is saved as .docm, not .docx. If the macro still does not run, open the VBA editor and verify the code is in the ThisDocument module of the correct document.
The File Name Contains Invalid Characters
Windows file names cannot contain the following characters: \ / : ? ” < > |. If your custom property or date format includes any of these, the SaveAs2 method will fail. Ensure your date format uses only hyphens or underscores. For example, use yyyy-mm-dd instead of yyyy/mm/dd. Check the custom property value for invalid characters before saving.
The Custom Property Is Empty After Saving
Custom document properties are stored in the document itself. If you open a document that was saved without the property, the macro will use the fallback value NOCODE. To fix this, add the custom property again using File > Info > Properties > Advanced Properties > Custom tab. For templates, add the property in the template so every new document inherits it.
Manual Naming vs Automated Macro: Key Differences
| Item | Manual Save As | Automated Macro |
|---|---|---|
| Effort per save | Type or paste file name each time | Zero typing after initial setup |
| Error rate | High, especially with dates and codes | Low, consistent format every time |
| Learning curve | None required | Requires VBA knowledge or copying code |
| Security restrictions | None | Macro must be enabled, file must be .docm |
| Flexibility | You can change the name freely | Convention is fixed; changes require code edit |
You can now save Word documents with a consistent file naming convention using either a manual field-based approach or an automated macro. Start by defining your convention and adding a custom document property for the static part. For frequent use, set up the AutoText entry so you can insert the skeleton quickly. If you manage many documents, the macro saves the most time. As a next step, explore the Document_Open event to display a dialog that lets the user choose the version number or project code before saving.