Document Editing Problems

How to Display a Word Filename Without the Extension

Ayan MasoodAyan Masood Oct 28, 2026 869 views

Question details

The user wants to insert the document's filename into a file without showing the file extension (such as .docx).

How to Display a Word Filename Without the Extension
Product
Microsoft Word
Device & OS
not provided
Scenario
Inserting a dynamic filename field into a document header, footer, or body for printing or sharing.
Observed behavior
The default FILENAME field always includes the file extension, requiring a workaround to hide it and display only the core document name.
Before you start

Ensure that your document has been saved at least once so it has a valid filename, and verify that macros are enabled in your word processor's security settings.

Solution 1Recommended

Use a DOCVARIABLE Field and VBA Macro

The most effective way to strip the file extension is by combining a document variable with a short VBA script.

Since the standard FILENAME field does not have a built-in switch to hide the extension, you can use a custom Document Variable (DOCVARIABLE) populated by a VBA macro. This macro reads the filename, removes the extension, and updates the document fields.

1
Insert the DOCVARIABLE Field

Click where you want the filename to appear. Press Ctrl+F9 to insert field braces { }, and type 'DOCVARIABLE FileName' inside them. It should look like { DOCVARIABLE FileName }.

2
Open the VBA Editor

Press Alt+F11 to open the Visual Basic for Applications (VBA) editor. Go to Insert > Module to create a new macro module.

3
Add the Macro Code

Paste the following code: Sub UpdateFileName() With ActiveDocument: .Variables("FileName").Value = Left(.Name, InStrRev(.Name, ".") - 1): .PrintPreview: .ClosePrintPreview: End With End Sub

4
Run the Macro

Close the VBA editor and run the macro by pressing Alt+F8, selecting 'UpdateFileName', and clicking Run. The print preview toggle in the code forces the fields to refresh, displaying your filename without the extension.

Use a DOCVARIABLE Field and VBA Macro
Automate the Process: You can assign this macro to a keyboard shortcut or add it to the Quick Access Toolbar for quick updates whenever the file is renamed.
Advanced Document Editing

Manage Fields and Macros Easily with WPS Writer

WPS Office Writer provides robust support for document fields, variables, and VBA macros, allowing you to automate tasks like inserting filenames without extensions effortlessly.

  1. 1. Insert a Field: Open your document in WPS Writer and press Ctrl+F9 to insert a field.
  2. 2. Define the Variable: Type 'DOCVARIABLE FileName' within the curly braces.
  3. 3. Run the Macro: Go to the Developer tab, click on Macros to insert your VBA script, and run it to strip the extension.
  4. 4. Update Fields: Right-click the field and select 'Update Field' to see your clean filename.
Fully compatible with Microsoft Word .docx formatsVBA macro support available for advanced automationFree, lightweight, and easy-to-use Office alternativeFamiliar tabbed interface for seamless migration
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't the default FILENAME field have a switch to hide the extension?

The standard FILENAME field in Word does not include a built-in formatting switch to remove the file extension. It only pulls the exact file name as saved on the operating system, which is why a VBA workaround is necessary.

Will this macro update automatically if I rename the file?

No, document variables and fields do not update automatically when a file is renamed outside the application. You must open the document and run the macro again to refresh the DOCVARIABLE value.

Can I use this method in headers and footers?

Yes, you can place the DOCVARIABLE field in the header or footer. The macro's print preview toggle (.PrintPreview followed by .ClosePrintPreview) is specifically designed to force fields in headers and footers to update.

Are macros safe to use in my documents?

Macros are safe if you write them yourself or obtain them from a trusted source. Because malicious code can be distributed via macros, always ensure your macro security settings are configured properly and only run macros you trust.