logo
search
VBA & Macro Problems

How to Extract Text from a Filename Using Word VBA Macros

WPS EditorWPS Editor Sep 28, 2026 871 views

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.

How to Extract Text from a Filename Using Word VBA Macros
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Open your Word document and press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the VBA editor, click on 'Insert' in the top menu and select 'Module'. This will open a blank code window.

3
Paste the VBA Code

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

4
Run the Macro

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 InStr Function to Extract Text Between Parentheses
Tip: You can assign this macro to a custom button on your Quick Access Toolbar for faster access in the future.

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. 1. Open Your Document in WPS Writer: Launch WPS Office and open your .docx or .docm document containing the formatted filename.
  2. 2. Enable the Developer Tab: Navigate to the top ribbon. If not already visible, go to Settings to enable the Developer Tools.
  3. 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.
Fully compatible with Microsoft Word .doc, .docx, and .docm formatsSupports advanced document automation and developer toolsLightweight, fast, and highly efficient for text processingFamiliar ribbon interface makes finding Developer tools easy
microsoft office alternative - wps office

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.