Syntax doubt - Find and Replace

H

Hari Prasadh

Hi,

Some time back I had a requirement where I had to REPLACE some text in EACH
PAGE of the MAIN STORY IN MS Word. The text was of the form -- Page 83 --
or -- Page 62-- and so on and the replacement text was the related to the
actual word page number.

The below macro I got from NG for this purpose works great. Only change is
that previously I had to find and replace text with 2 digit numbers
following "Page ". But now I want to be able to replace 3 digit numbers
also.

I have tried changing the syntax -- .Text = "Page [0-9]{1,2}" -- to --
..Text = "Page [0-9]{1,2,3}" -- thinking that this would extend the search to
one more character, but Im getting -- Runtime error '5560': The find what
text contains a pattern match expression which is not valid.-- and the above
error occurs at the -- .Execute Replace:=wdReplaceAll--.

Strangely if I change it to .Text = "Page [0-9]{3}" -- then am able to
get it working (that is am able to search for -- Page 768 -- and replace it
with the desired text).

Why is it that .Text = "Page [0-9]{1,2}" works well for finding -- Page
83 -- or -- Page 62-- but .Text = "Page [0-9]{1,2,3}" -- gives syntax
error. Isnt the number within the squiggly bracket meant to indicate the
extent of the search?


Thanks a lot,
Hari
India

Sub ReWritingPageNumbersByHelmutWeber()
'Swiped from NG by Hari Prasadh
ResetSearch
Dim lPgs As Long
Dim lTmp As Long
lPgs = Selection.Information(wdNumberOfPagesInDocument)
For lTmp = 777 To lPgs
Selection.GoTo _
what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=lTmp
Selection.Bookmarks("\page").Select
With Selection.Find
.Text = "Page [0-9]{1,2}"
.Replacement.Text = "Page " & lTmp - 20
.Wrap = wdFindStop
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Next
ResetSearch
End Sub

'---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more
.Execute
End With
End Sub
 
H

Hari Prasadh

Hi,

I see that using -- .Text = "Page [0-9]{3}" -- would enable me to
change 3 digit numbers following Page -- Page 789-- but if I have a 2 digit
number or 1 digit number it doesnt work. So i think I would have to use
Syntax of the form ---- .Text = "Page [0-9]{1,2,3}" -- in order to get
it work, but then Im getting error number 5560 if I use it.

Actually I want the search to be that if it cant find a 3 digit number
following the text page, then it searches whether a 2 digit number following
the text page exists.

For example if My Main Story runs like -- Information on Paris can be found
in Page 567,while information on Cancer can be found in Page 45. In this
case Page 567 and page 45 are candidates for replacement.

Please suggest a way around the same.

Thanks a lot,
Hari
India

news:[email protected]...
 
H

Helmut Weber

Hi Hari,

..Text = "Page [0-9]{1,3}"
Search for 1 to 3 digits.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 

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