View Single Post
 
Old 06-13-2018, 08:58 AM
d4okeefe d4okeefe is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

With VBA, you can probably simply find and replace with wildcards, rather than use the <Microsoft VBScript Regular Expressions 5.5> library. Although you could use that too. The advantage of regex, in my experience, is that it allows you to identify substrings of zero or more instances, whereas Word's wildcards do not.

For the example you give, you can do something like this with Wildcards

Code:
Sub find_replace_yearbook()
    Dim d As Document: Set d = ActiveDocument
    Dim r As Range: Set r = d.Content
    
    Dim Author1 As String, ArticleTitle1 As String
    Author1 = "NameOfAuthor"
    ArticleTitle1 = "NameOfArticle"
    
    r.Find.ClearFormatting
    r.Find.Replacement.ClearFormatting
    r.Find.Text = "yearbook ([0-9]{4}, p. )[0-9]{1,}"
    r.Find.MatchWildcards = True
    r.Find.Replacement.Text = Author1 + ", " + ArticleTitle1 + ", YB \1" + "2-137"
    r.Find.Execute Replace:=wdReplaceAll
End Sub
Reply With Quote