Automated way to change numbers in Word?

Question: Is there any way to get Word to find and replace a varied range of numbers by 1?

I am using Word 2010 on Windows 7.

I have a large document that contains about 300 cites to documents that will be submitted with the document. Each cite reads as "Exh. X" (where X is a number, 1 through 300). So the text might read as follows:

The car is brown. (Exh. 1). The house is red. (Exh. 2). The house is small. (Exh. 3). The car was painted Brown by Jack. (Exh. 1). The car is broken. (Exh. 4). 

The exhibits sometimes change. So Exh. 2 might drop out entirely, leaving a gap from Exh. 1 to Exh. 3. I then need to go through and reduce every exhibit number greater than 2 by one. So Exh. 3 becomes Exh. 2, Exh. 4 becomes Exh. 3, etc. When done manually, this is very time consuming.

If each exhibit only appeared once, I could easily make this change using a field, finding and replacing all the Exh. X entries with the field, and then updating it. But I only know how to do this sequentially (i.e., once updated, the exhibits will run 1 through however many Exh. entries there are). However, as the example above shows, some exhibits appear later in the document and must be referenced by the earlier exhibit number. So Exh. 1 appears first, but it then reappears after Exh. 3. If I were to update this text using a sequential list after Exh. 2 dropped out, then "The car was painted Brown by Jack (Exh. 1)" would instead be marked with Exh. 3.

Is there anyway to get Word to automatically reduce every Exh. number greater than 2 by 1 (or any value)? 

Any suggestions would be very much appreciated!

Answer
Answer
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim lngNum As Long
lngNum = Val(InputBox("What is the number index?", "Index"))
Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "Exh. [0-9]{1,}"
    .MatchWildcards = True
    While .Execute
      oRng.Start = oRng.Start + 5
      If Val(oRng) > lngNum Then
        oRng = oRng - 1
      End If
      oRng.Collapse wdCollapseEnd
    Wend
  End With
 
End Sub
Greg Maxey
***
Death smiles at us all, but all a man can do is smile back.


For more help with Word visit:
http://gregmaxey.com/word_tips.html

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 78 Applies to: