VBA & Macro Problems

Create a Word Macro to Save a Form with a Date-Based Filename

Nimra MalikNimra Malik Nov 22, 2026 869 views

Question details

Create a Word macro that validates required form fields and saves the form with field values and the current date and time in its filename.

Create a Word Macro to Save a Form with a Date-Based Filename
Product
Microsoft Word
Device & OS
not provided
Scenario
A user completes a Word form and runs a macro to save it under a descriptive, date-based filename.
Observed behavior
The form should save only after required fields are filled in, using a Word document format appropriate for its macros.
Before you start

Add text or date content controls to the form and give the required controls the titles FormDate and Technician in Developer > Properties. Save the document as a macro-enabled Word document (.docm) before adding the code.

Solution 1Recommended

Validate the form and save it with a timestamped filename

Use this VBA macro when the form has titled content controls and the saved document needs to retain its macros.

The macro checks both required controls, exits immediately if either is missing or empty, and asks the user to choose a save folder. It replaces characters that are not allowed in filenames.

1
Open the VBA editor

In Word, open the form and select Developer > Visual Basic. In the editor, select the document's VBA project, then choose Insert > Module.

2
Add the macro

Paste the following code into the module: Option Explicit Public Sub SaveCompletedForm() Dim dates As ContentControls, techs As ContentControls Dim dateValue As String, techValue As String Dim folder As String, fileName As String Dim picker As FileDialog Set dates = ActiveDocument.SelectContentControlsByTitle("FormDate") Set techs = ActiveDocument.SelectContentControlsByTitle("Technician") If dates.Count = 0 Or techs.Count = 0 Then MsgBox "The FormDate or Technician control is missing." Exit Sub End If If dates(1).ShowingPlaceholderText Or techs(1).ShowingPlaceholderText Then MsgBox "Enter the form date and technician name before saving." Exit Sub End If dateValue = Trim$(dates(1).Range.Text) techValue = Trim$(techs(1).Range.Text) If Len(dateValue) = 0 Or Len(techValue) = 0 Then MsgBox "Enter the form date and technician name before saving." Exit Sub End If Set picker = Application.FileDialog(msoFileDialogFolderPicker) picker.Title = "Choose a folder for the completed form" If picker.Show <> -1 Then Exit Sub folder = picker.SelectedItems(1) If Right$(folder, 1) <> Application.PathSeparator Then folder = folder & Application.PathSeparator End If fileName = "Form_" & SafeFilePart(dateValue) & "_" & _ SafeFilePart(techValue) & "_" & Format$(Now, "yyyymmdd_hhnnss") & ".docm" ActiveDocument.SaveAs2 FileName:=folder & fileName, _ FileFormat:=wdFormatXMLDocumentMacroEnabled End Sub Private Function SafeFilePart(ByVal value As String) As String Dim bad As Variant, item As Variant, i As Long bad = Array("\", "/", ":", "*", "?", Chr$(34), "<", ">", "|") For Each item In bad value = Replace$(value, CStr(item), "-") Next item For i = 0 To 31 value = Replace$(value, Chr$(i), "-") Next i SafeFilePart = Trim$(value) End Function

3
Test the required fields

Return to the form, leave one required control blank, and run Developer > Macros > SaveCompletedForm > Run. Confirm that the message appears and no save occurs.

4
Save a completed form

Fill in both controls, run Developer > Macros > SaveCompletedForm > Run, and select a folder. Check that the resulting .docm filename contains the field values and timestamp.

Validate the form and save it with a timestamped filename
Manual saves are separate: This macro validates the form only when it runs. A user can still save manually without running it. Intercepting manual saves requires an initialized application-level DocumentBeforeSave event handler; it is not included in this example.
Free Microsoft Office alternative

Work with Word documents in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening, editing, and saving Word documents. Its format support makes it easy to move everyday documents between office suites, though it does not fix Microsoft Word VBA event or security issues.

Edit and save form documents in a familiar Writer interfaceOpen and save Microsoft Word formats such as .docxUse a free, lightweight office suiteMove everyday documents between WPS Office and Microsoft Office
microsoft office alternative - wps office

Frequently Asked Questions

Why does the macro save even when a required field is blank?

A MsgBox alone does not stop a macro. Add Exit Sub immediately after the missing-information message so execution stops before ActiveDocument.SaveAs2.

How do I name the form controls used by the macro?

Select each content control in Word, open Developer > Properties, and set its Title to FormDate or Technician. The sample finds controls by those titles.

Should the saved form be .docx or .docm?

Use .docm with wdFormatXMLDocumentMacroEnabled if the saved document must retain VBA macros. A .docx file does not retain VBA code.

Can this macro stop someone from saving the form manually?

No. Developer > Macros runs the validation, but a manual save can bypass it. Intercepting manual saves requires an application-level DocumentBeforeSave event handler that is initialized while Word runs.

What if Word blocks the macro?

Check Word's macro security settings and your organization's policy. Depending on the source and platform, you may need to unblock the file, use a trusted location, or trust a signing certificate. Enable macros only from a source you trust.