View Single Post
 
Old 01-11-2022, 04:30 PM
AlexCummins AlexCummins is offline Windows 10 Office 2007
Novice
 
Join Date: Jan 2022
Posts: 2
AlexCummins is on a distinguished road
Default

Many thanks for this!

I now see from applying your item 3 above that Word is inserting some sort of hidden special character at the boundaries of the string marked with a comment. Item 3 above on its own solves it.

This also allows me to work out a (still clunky, but workable) solution to where strings are not nested (where I can use the system of calling the longer strings before the shorter ones), but overlap, such as for the strings:
  • going without
  • without you
  • you are

The best way I can think of to search for "without you" with the possibility of hidden special characters after "without" (after a comment has been applied to "going without") and/or before "you" (after a comment has been applied to "you are") is to have three variants of the "without you" macro (01 in the case where there is no special character in the string, 02 where there is one plus a space, and 03 where there are two plus a space):

Sub CallMacros()
Call Without
Call WithoutYou01
Call WithoutYou02
Call WithoutYou03
End Sub

Sub Without()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without", False) = True
ActiveDocument.Comments.Add range, "This is string: without"
Loop
End Sub

Sub WithoutYou01()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without you", False) = True
ActiveDocument.Comments.Add range, "This is string: without you"
Loop
End Sub

Sub WithoutYou02()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without^?^?you", False) = True
ActiveDocument.Comments.Add range, "This is string: without you"
Loop
End Sub

Sub WithoutYou03()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without^? ^?you", False) = True
ActiveDocument.Comments.Add range, "This is string: without you"
Loop
End Sub

I couldn't find a cleaner way to do this (e.g. ignoring special characters in a search string, or using wildcards within a string), but this does work.

I'll work with this for now as it does the job. If there's a better way of doing it, grateful for any further input. Thanks again!
Reply With Quote