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
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