How to Extract Text from a Filename Using Word VBA Macros
Question details
The user wants to extract specific text enclosed in parentheses from the active document's filename and insert it directly into the document at the current cursor position.

- Product
- Microsoft Word / WPS Writer
- Device & OS
- not provided
- Scenario
- Automating text extraction from a filename to populate document content, specifically targeting data enclosed within parentheses.
- Observed behavior
- The user needs a functional VBA macro script that reads the filename, dynamically identifies the text between the opening and closing parentheses, and types it into the active document.
Ensure your document has been saved at least once so it has a valid filename containing parentheses, and verify that the Developer tab is enabled to run VBA macros.
Use the InStr Function to Extract Text Between Parentheses
This method uses the InStr function to precisely locate the opening and closing parentheses and extracts the text between them using the Mid function.
This is the most robust method for extracting targeted text because it calculates the exact character positions of the parentheses before attempting extraction, preventing errors if parentheses are missing.
Open your Word document and press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.
In the VBA editor, click on 'Insert' in the top menu and select 'Module'. This will open a blank code window.
Copy and paste the following code into the module window: Sub InsertPartOfFilename() Dim strName As String Dim p1 As Long Dim p2 As Long strName = ActiveDocument.Name p1 = InStr(strName, "(") If p1 > 0 Then p2 = InStr(p1 + 1, strName, ")") If p2 > 0 Then strName = Mid(strName, p1 + 1, p2 - p1 - 1) Selection.TypeText strName End If End If End Sub
Close the VBA editor. Place your cursor inside the document where you want the text to appear. Press ALT + F8, select 'InsertPartOfFilename', and click 'Run'.

Use the Split Function for Simpler Code
An alternative, more concise approach that uses the Split function to divide the filename string at the parentheses.
Automate Document Tasks with WPS Office
WPS Office offers robust support for macros and developer tools, allowing you to seamlessly automate repetitive tasks like extracting filename data. Enjoy high compatibility with Microsoft Word documents, scripts, and a familiar user interface.
- 1. Open Your Document in WPS Writer: Launch WPS Office and open your .docx or .docm document containing the formatted filename.
- 2. Enable the Developer Tab: Navigate to the top ribbon. If not already visible, go to Settings to enable the Developer Tools.
- 3. Insert and Run the Macro: Click on 'Macros' or open the VBA Editor, paste the extraction script, and run it directly in your document.

Frequently Asked Questions
Why is the macro returning an error or doing nothing?
If the document has not been saved yet, it will have a generic name like 'Document1' without the target parentheses. You must save the file first so that 'ActiveDocument.Name' contains the correct formatting.
How can I extract text from brackets instead of parentheses?
You can easily modify the script by changing the target characters. In the VBA code, replace "(" with "[" and ")" with "]" within the InStr or Split functions.
Can I extract the file extension along with the filename?
Yes. If you simply want the entire filename including the extension, you can skip the text parsing functions and directly use 'Selection.TypeText ActiveDocument.Name'.
How do I run this macro automatically when the document opens?
You can rename the macro subroutine from 'InsertPartOfFilename()' to 'AutoOpen()', or call your macro directly from the 'Document_Open' event located in the 'ThisDocument' module.




