How to Fix Word VBA Syntax Errors for Date-Based Filenames
Question details
Users experience syntax errors when directly using Word VBA Format expressions to save files or when formatting strings contain unescaped path characters.

- Product
- Word VBA
- Device & OS
- not provided
- Scenario
- Automating document saves with dynamic date-based filenames using the SaveAs2 method in VBA.
- Observed behavior
- Executing the macro triggers a syntax error due to improper handling of the Format function's return value or invalid formatting characters like unescaped backslashes.
Ensure that your VBA project is not currently in break mode and that you have enabled the Developer tab to access the Visual Basic Editor.
Assign the Formatted Filename to a Variable
The most reliable way to avoid syntax errors with SaveAs2 is to assign the formatted date string to a variable before passing it to the method.
When passing a Format expression directly as an argument to a method like SaveAs2, VBA can struggle to evaluate the syntax if parentheses or escape characters are not perfectly aligned. Assigning the result to a string variable first ensures the path is evaluated properly.
Press Alt + F11 to launch the Visual Basic Editor and locate your saving macro.
Add a variable declaration at the top of your subroutine, for example: Dim strFile As String.
Construct the filename and assign it to your variable. Ensure you escape backslashes if used as literal characters. Example: strFile = Format(Date, "yyyy\\mmmm\\dd-mmmm-yyyy") & ".docx"
Update your save method to use the new variable: ActiveDocument.SaveAs2 FileName:=strFile
Insert Hidden Date and Time Stamps correctly
If you are inserting dates directly into the document rather than saving files, use the InsertDateTime method and modify font visibility.
Run and Edit VBA Macros Seamlessly in WPS Office
WPS Office provides excellent support for VBA macros, allowing you to run your customized Word scripts. Its intuitive developer tools make automating formatting and saving tasks faster and easier.
- 1. Enable Developer Tools: Open WPS Writer, go to the top ribbon, and navigate to the Developer tab.
- 2. Open the VBA Editor: Click on 'Macros' or press Alt + F11 to launch the built-in Visual Basic Editor.
- 3. Paste Your Code: Paste your corrected VBA code assigning the formatted filename to a string variable.
- 4. Execute the Macro: Click the Run button or press F5 to execute your macro and save your document dynamically.

Frequently Asked Questions
Why do backslashes in a VBA Format string cause syntax errors?
In VBA, backslashes inside a Format string function as escape characters. If they are used to represent file paths without being properly escaped themselves, VBA may misinterpret the following characters, leading to a syntax error. It is often safer to format the date string first, then concatenate it with your directory path string.
Can I pass a Format expression directly to SaveAs2?
While it is technically possible in some scenarios, doing so frequently results in evaluation and syntax errors due to unmatched parentheses or improper string concatenation. Assigning the Format result to a string variable first resolves this issue.
How do I format the current time in a VBA macro?
You can use the Format function alongside the Now keyword. Depending on your preference, use Format(Now, "HH:mm:ss") for a 24-hour clock or Format(Now, "hh:mm:ss AM/PM") for a 12-hour clock.




