Others

Use Word VBA Wildcards to Copy Matching References to a New Document

Adam DavisAdam Davis Nov 25, 2026 868 views

Question details

Copy references such as NRL- followed by digits from a Word document into a new document using a VBA wildcard search.

Use Word VBA Wildcards to Copy Matching References to a New Document
Product
Microsoft Word
Device & OS
not provided
Scenario
A recorded macro needs to find and copy every matching reference, rather than relying on the current Selection.
Observed behavior
The recorded macro may work only once because subsequent searches depend on the current Selection.
Before you start

Open the source document in Microsoft Word and confirm that the references use the NRL- prefix followed by one or more digits. Make sure you can open the VBA editor and run macros under your organization's security settings.

Solution 1Recommended

Use a Word VBA Range to Find and Copy Each Reference

Use this approach when you need a repeatable macro that copies all matching references with their formatting.

The wildcard pattern NRL-[0-9]{1,} finds NRL- followed by one or more digits. After each match, the macro collapses the source range to its end and extends it to the end of the source document before searching again.

1
Open the VBA editor

With the source document active in Word, open Developer > Visual Basic. In the VBA editor, choose Insert > Module.

2
Add the macro

Paste this code into the module: Sub CopyNRLReferences() Dim srcDoc As Document Dim dstDoc As Document Dim searchRange As Range Dim dstRange As Range Set srcDoc = ActiveDocument Set searchRange = srcDoc.Content Set dstDoc = Documents.Add With searchRange.Find .ClearFormatting .Text = "NRL-[0-9]{1,}" .Forward = True .Wrap = wdFindStop .MatchWildcards = True End With Do While searchRange.Find.Execute Set dstRange = dstDoc.Range(dstDoc.Content.End - 1, dstDoc.Content.End - 1) dstRange.FormattedText = searchRange.FormattedText dstDoc.Range(dstDoc.Content.End - 1, dstDoc.Content.End - 1).InsertAfter vbCr searchRange.Collapse Direction:=wdCollapseEnd searchRange.End = srcDoc.Content.End Loop End Sub

3
Run and review the results

Return to the source document, choose Developer > Macros, select CopyNRLReferences, and click Run. Review the new document and use File > Save As to save it.

Use a Word VBA Range to Find and Copy Each Reference
Search scope: This macro searches the source document's main text range. It does not separately search headers, footers, or other document stories.
Free Microsoft Office alternative

Edit and Review Reference Documents in WPS Writer

WPS Office is a free, lightweight alternative for opening, editing, and reviewing Word documents in a familiar interface. Its format support makes it easy to move existing documents between applications; use Microsoft Word to run the VBA macro above.

Review and edit documents containing NRL referencesOpen and save Microsoft Word DOC and DOCX filesWork in a familiar, easy-to-use interfaceUse a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does the recorded macro find a reference only once?

A recorded macro can depend on the current Selection, which may not be positioned for the next search. A Range-based search can collapse to the end of each match and continue through the source document.

What does the wildcard pattern NRL-[0-9]{1,} match?

In a Word Find search with MatchWildcards enabled, it matches NRL- followed by one or more digits, such as NRL-7 or NRL-123.

Does the macro preserve the formatting of each match?

Yes. The macro assigns each matching range's FormattedText to the destination range, then inserts a paragraph break after it.

Where are the copied references saved?

The macro places them in a new, unsaved Word document. In that document, choose File > Save As to select a name and location.