G
Greg Maxey
I need to perform a find and replace operation on text. Some of the "find"
words are also bookmarked. When the replace operation takes place the
bookmarks are waxed.
Example: This test is a test to test this macro. Each "test" is
bookmarked. If I run
Sub Test()
Dim myRange As Range
Set myRange = ActiveDocument.StoryRanges(wdMainTextStory)
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Test"
.Replacement.Text = "Test"
.Replacement.Font.Bold = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End Sub
All of the bookmarks are waxed.
The only solution I can find is is to first grow the range of each bookmark
to include the following space then do the replacement.
Sub GrowBM()
Dim bMark As Bookmark
Dim myRange As Range
For Each bMark In ActiveDocument.Bookmarks
Set myRange = bMark.Range
If bMark.Range.Characters.Last <> Chr(32) Then
myRange.MoveEnd Unit:=wdCharacter, Count:=1
myRange.Select
ActiveDocument.Bookmarks.Add Name:=bMark.Name, Range:=Selection.Range
End If
Next
End Sub
Does anyone have a better suggestion?
Thanks.
words are also bookmarked. When the replace operation takes place the
bookmarks are waxed.
Example: This test is a test to test this macro. Each "test" is
bookmarked. If I run
Sub Test()
Dim myRange As Range
Set myRange = ActiveDocument.StoryRanges(wdMainTextStory)
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Test"
.Replacement.Text = "Test"
.Replacement.Font.Bold = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End Sub
All of the bookmarks are waxed.
The only solution I can find is is to first grow the range of each bookmark
to include the following space then do the replacement.
Sub GrowBM()
Dim bMark As Bookmark
Dim myRange As Range
For Each bMark In ActiveDocument.Bookmarks
Set myRange = bMark.Range
If bMark.Range.Characters.Last <> Chr(32) Then
myRange.MoveEnd Unit:=wdCharacter, Count:=1
myRange.Select
ActiveDocument.Bookmarks.Add Name:=bMark.Name, Range:=Selection.Range
End If
Next
End Sub
Does anyone have a better suggestion?
Thanks.