Hi,
I have a piece of code which does the following when closing a word document:
1) Selct a range.
2) Take a duplicate of that range, with the duplicate range:
3)DO a wildcard find to find all highlighted characters.
4)Once a character is found, select the entire word , check if it has a
bookmark starting wit a certain name, if not unhighlight it.
6)Move the duplicate range by a word( range.move wdWord) and make the end of
the range = the end of the original range.
5) Do this until, no highlights are found, or end of document (wdFindStop).
This works in all scenarios except when I encounter form fields with the
bookmarks I dont want to unhighlight. In such cases Range.move does not move
forward and keeps on looping on the same word. What is the way out of this?
This is extremely critical as this code is in production and is now showing
these symptoms. I would appreciate any help on the matter. The code snippet
is given below:
Public Function RemoveHighlights() As Long
On Error GoTo ErrHandler
......
Dim oRange As Range
Dim BMName As String
Dim Count As Long
Dim i As Long
Dim isMark As Boolean
Set oRange = ActiveDocument.Range(wdStory)
' late binding for Find to take care of a Word XP bug
Dim rngResult As Object
Set rngResult = oRange.Duplicate
' Loop to find all yellow highlights left,
With rngResult.Find
.ClearFormatting
.Highlight = True
.Text = "?" 'going by each character because going by words can miss
the highlights in some cases eg: all letters of the word except one are
highlighted, MSWord does not see the word as highlighted and skips it
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While rngResult.Find.Execute
' Exit loop if not found
If Not rngResult.Find.Found Then Exit Do
isMark = False
'select the found character, and then select the entire word based
on it, so that processing ca be faster
rngResult.Select
Application.Selection.Words.Item(1).Select
'see if word has the specific bookmark name
Count = Application.Selection.Bookmarks.Count
If Count > 0 Then
'do a check for the bookmark, if foun isMark = true
End If ' end of if count>0
' verify the highlighted bookmark is not isMark, then unhiglight it
If Not isMark Then
Application.Selection.Range.HighlightColorIndex = wdNoHighlight
End If
'reset range to be after the currently unhighlighted word
rngResult.MoveStart wdWord
If rngResult.Start >= oRange.End Then
Exit Do
Else
' and extending the end of rngResult
rngResult.End = oRange.End
End If
Loop ' DO loop
End With
ErrHandler:
'error handling and return values here
End Function
jagdeep singh - 25 Dec 2004 22:35 GMT
one problem I see with my code is that I do : rngResult.MoveStart wdWord
if the form field has many word ( as in most cases for my work), we move by
a word (still inside the form field), and then in the next iteration of the
loop , when i select, the code selects the entire form field again, thus
making it go in a loop. How could you move the start of a range to the end of
the form field??!!
Another related problem is the same scenario inside a header cell, say my
cell has [form field] then some highlighted text
wdWord again does not seem to work!!! HELP!!
> Hi,
>
[quoted text clipped - 76 lines]
> 'error handling and return values here
> End Function
jagdeep singh - 28 Dec 2004 01:51 GMT
well, I did a work around, where I first keep tab of the bookmarks i need to
keep highlighted ( in my case, in an XMl file).
Then, I proceed to define a range and for the entire ranhe
highlightcolorindex = wdNoHighlight
then i re highlight the bookmarks I wanted highlighted by referencing their
names from the xml file
If ActiveDocument.Bookmarks.Exists(BookmarkName) Then
ActiveDocument.Bookmarks(BookmarkName).Range.HighlightColorIndex =
wdYellow
End If
seems to work and less complicated.
jagdeep
> Hi,
>
[quoted text clipped - 76 lines]
> 'error handling and return values here
> End Function