I've got a bunch of regulations, and I have a regular expression that finds them. They all look like "28-35-###(sometimes letter here)". I have all of them as bookmarked as the text KAR28_35_###(sometimes letter here).
I would like to find all instances of the text K.A.R. 28-35-###(a-z)? and tack on a "[p. { PAGEREF KAR28_35_###(a-z)? \h }]" on the end so it ends up looking like "K.A.R. 28-35-144a [p. 35]."
But when I do find/replace, I can't insert the field curlycues, just regular curly brackets. It's only text.
Is there a way I can get it to insert an actual field?
I've got a bunch of regulations, and I have a regular expression that finds them. They all look like "28-35-###(sometimes letter here)". I have all of them as bookmarked as the text KAR28_35_###(sometimes letter here).
I would like to find all instances of the text K.A.R. 28-35-###(a-z)? and tack on a "[p. { PAGEREF KAR28_35_###(a-z)? \h }]" on the end so it ends up looking like "K.A.R. 28-35-144a [p. 35]."
But when I do find/replace, I can't insert the field curlycues, just regular curly brackets. It's only text.
Is there a way I can get it to insert an actual field?
Regex can only work with text strings, not formatting or other non-text data such as field braces. You could, however, use a Word macro like:
Sub InsertPageRefs()
Application.ScreenUpdating = False
Dim StrTxt As String
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "K.A.R. 28-35-[0-9a-z]@>"
.Replacement.Text = ""
.MatchWildcards = True
.Wrap = wdFindStop
.Forward = True
.Format = False
End With
Do While .Find.Execute
StrTxt = "PAGEREF KAR_28_35_" & Split(.Text, "-")(2) & " \h"
With .Duplicate
.Collapse wdCollapseEnd
.Text = " [p. ]"
.Fields.Add Range:=.Characters.Last.Previous, Type:=wdFieldEmpty, Text:=StrTxt, PreserveFormatting:=False
End With
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
End Sub
To make the code even more flexible, so that it finds all the K.A.R. ##-##-###? sequences, you could use:
.Text = "K.A.R. [0-9]{2}-[0-9]{2}-[0-9a-z]@>"
or, for sequences like K.A.R. #-##-###? and K.A.R. ##-#-###? as well:
.Text = "K.A.R. [0-9]@-[0-9]@-[0-9a-z]@>"
and:
StrTxt = "PAGEREF KAR_" & Replace(Split(.Text, " ")(1), "-", "_") & " \h"