View Single Post
 
Old 07-18-2016, 04:58 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It was declared because that same range was needed 5 times and the code was completely different to your second question. Declaring a range makes it easier to follow the code and can reduce the number of contextual jumps required by the code. For instance
Selection.Range requires Word to first find out which document is active, then determine the start and finish positions of the selection in that document and then remember that range for the next command. Using a declared range reduces the 'thinking' required by the code.
Working with ranges also reduces the ambiguity of where the 'selection' is located at any time. If your code happens to change the selection: either in the activedocument or because you have opened/activated a different document, the Selection.Range will give a very different result and is probably not what you were expecting.

Now it just so happens that rng.Find is somewhat buggy and when something is found, the rng collapses to the found text. That is why your code doesn't 'find' anything underlined. In that case, you would need to redefine the range before doing the second find.

In answer to your question in the code - I would think that the second find is actually searching for underlined AND strikethrough text since you didn't clear the formatting after the first time. That is the point of the .clearformatting which removes any of the sticky formatting that might already be retained from a previous finds performed by the user prior to your code running. You would need to .clearformatting again to remove the strikethrough condition in the second find before applying the font.underline condition otherwise you are searching for text with both.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote