0
votes

I want to highlight certain strings (from a list) throughout a whole word document.

Easiest option seems to be find/replace. However, if the string I am looking for contains revision marks, then (probably due to some word bug) inside hyperlinks the following things will happen:

  • if the 1st character of the string is marked as a revision, the string won't be found at all

  • if any other character of the string is marked as deleted revision, the string will be found, but won't be completely selected. Instead the selection will be for the number of characters shorter that are marked as being deleted (for example if in 'strring' the 3rd character is marked as being a deleted revision, a search for 'string' will return the selection 'strin').

My second approach was to identify the terms by preceding the search string with a wildchard character (so as to find even those terms whose 1st character has been revisioned) and subsequently expand the selection to a whole word.

Dim strTerm as String
strTerm = "string"
Selection.Find.Execute FindText:="^?" & strTerm, _
  MatchCase:=False, MatchWholeWord:=False, _
  MatchAllWordForms:=False, Forward:=True, _
  Wrap:=wdFindStop, Format:=False
If Selection.Find.Found Then
    Selection.MoveStart Count:=1  'exclude wildchart character from found string
    Selection.Expand              'expand selection to whole word
end if

This will reliably find all terms, but if the term includes the 1st or last word inside a hyperlink, no matter what I do, I can not select it, I will always end up selecting the whole hyperlink field.

I need to leave the text intact with all revision marks unaltered. I can only add highlights.

Can anybody set me on the right track?

1

1 Answers

1
votes

I found an answer to my question. It isn't very elegant, but a practical work-around the mentioned word-bug (I assume that is is indeed a bug and no intentional behaviour)...

Dim strTerm as String       'input:  a term taken from a list
Dim strWholeTerm as String  'output: a word within the text that starts as strTerm 
                            '        but might differ in ending

'look for the next term from the list (here "string")
strTerm = "string"
Selection.Find.Execute FindText:="^?" & strTerm, _
  MatchCase:=False, MatchWholeWord:=False, _
  MatchAllWordForms:=False, Forward:=True, _
  Wrap:=wdFindStop, Format:=False
If Selection.Find.Found Then
    Selection.MoveStart Count:=1                      'exclude wildchart character
    Selection.Expand                                  'expand selection to whole word
end if

'check whether .Expand has selected the whole hyperlink
'and not just the word within which strTerm had been found
If Selection.Start < rngFoundString.Start Then         
    Options.DeletedTextMark = wdDeletedTextMarkDoubleStrikeThrough        'deleted revisons visible
    Set rngWholeTerm = Selection.Range                                    'get end of whole term
    rngWholeTerm.SetRange Start:=rngFoundString.Start, End:=rngWholeTerm.End 'define whole term
    rngFoundString.Select
    Selection.Collapse                                 'start search at beginning of whole term
    Selection.Find.Execute FindText:=rngWholeTerm.Text 'select whole term
    Options.DeletedTextMark = wdDeletedTextMarkHidden  'deleted revisions invisible
End If
'at this point the whole word - starting as strTerm - has been selected

Should anyone know a more straight forward method of reliably marking words from a list within a text (words that might have a range of different endings), including words located withing hyperlinks and including words that contain revision marks at random positions, I would be very curious to learn about it.