Find-and-replace with sequential numbering

B

Bert Coules

I have (fairly urgently) to edit a lengthy manuscript as follows:
throughout the text there are lines which begin with the string "??"
(without the quotation marks) . There can be anything from two to twenty of
these per page.

I have to replace them with numbers, such that the first example on each
page becomes 1, the next 2 and so on. As an extra, it would be pleasant if
single-digit numbers could be prefaced with a space.

I'm only just beginning to look at macros, let along get to grips with even
the fundamentals. I don't like to ask for a ready-made solution, but if
there's anything existing that would give me a clue as to how to do this, I
would very much appreciate a pointer towards it.

Many thanks,

Bert
http://www.bertcoules.co.uk
 
G

Greg

Bert,

Here is a rough macro that will get you there in a couple of steps. I
didn't have time to fully automte.

Finding the "??" and replacing with a number is easy. The challenge,
for me at least, was restarting the numbering at the first instance of
"??" on each page.

Part 1:
First copy this code in your VBA editor. Notice that one line is
stetted out. Don't delete that line as you will need it for part 2 :)

Sub ScracthMacro()
ActiveDocument.Range(0, 0).Select

Do
Selection.GoTo What:=wdGoToBookmark, _
Name:="\Page"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
..Text = "??"
..Replacement.Text = "^c"
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
'Selection.Find.Execute Replace:=wdReplaceAll
Selection.GoTo What:=wdGoToBookmark, _
Name:="\Page"
If Selection.Range.End + 1 = _
ActiveDocument.Range.End Then
Exit Do
End If
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveEnd Unit:=wdLine, Count:=1
Selection.GoTo What:=wdGoToBookmark, _
Name:="\Page"
Loop
ActiveDocument.Fields.Update

End Sub

Now any where in your document. Enter the following SEQ field:

{ SEQ Num \r 1 \# "0#" }

Note: the bracket pair { } is entered with CTRL+F9

Toggle the field code. Carefully select just the displayed characters
and press CTRL+c

Run the macro,

Your first "??" instance on each page should be 01.

Part 2.

In the VB editor, remove the stet mark form the line of code and stet
the preceeding line.

Now any where in your document. Enter the following SEQ field:

{ SEQ Num \# "0#" }

Toggle the field code. Carefully select just the displayed characters
and press CTRL+c

Delete the two field codes that you created.

Run the macro again.

You can use find and replace to convert the 01, 02 to space 1 space 2
if you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top