Use Word VBA Wildcards to Copy Matching References to a New Document
Question details
Copy references such as NRL- followed by digits from a Word document into a new document using a VBA wildcard search.

- 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.
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.
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.
With the source document active in Word, open Developer > Visual Basic. In the VBA editor, choose Insert > Module.
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
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.

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.

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.




