Find/Replace macro only limited to selection when entire paragraph is selected

December 23rd, 2011 - 08:32 am ET by Paul | Report spam
Hello,

I've created a macro in Word 2007 that is designed to apply special
formatting to the current selection. When an entire paragraph is
selected, it works beatifully. However, when a few words within the
paragraph are selected, the changes continue beyond the selection until
the end of the document.

Here is the VBA code:

Sub applyNewRevisedText(control As IRibbonControl)
'
' applyNewRevisedText Macro
'
' This macro applies a magenta font color
' and Italic character formatting to the selected text.
' Bold, Bold Italic, Italic, Underlined, and Emphasis
' text is maintained so that when the New/Revised text
' formatting is removed, these formatting attributes
' are maintained.
'

' Change all instances of the Default Paragraph Font style
' to New/Revised Text.
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Default Paragraph Font")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"New/Revised Text")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

' Change all instances of the Emphasis character style
' to New/Revised Emphasis.
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Emphasis")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"New/Revised Text Emphasis")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

' Change all instances of the Bold character style
' to New/Revised Bold.
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Bold")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"New/Revised Text Bold")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub

You can see that what I'm trying to do is a little more complex than
simply exchanging one style for another in the entire selection. Rather,
I need to tag the special character styles so that they are properly
maintained while the "New/Revised" formatting is applied. Then, I have a
corresponding macro that will reverse the process and reapply the
"standard" styles.

Any advice on how to make this macro apply the formatting to *only* the
selection, even if only a word or two in a paragraph are selected?

Any help is greatly appreciated.

Paul Burton
email Follow the discussionReplies 22 repliesReplies Make a reply

Similar topics

Replies

#1 Paul
December 23rd, 2011 - 10:00 am ET | Report spam
On 2011-12-23, Paul wrote:
I've created a macro in Word 2007 that is designed to apply special
formatting to the current selection. When an entire paragraph is
selected, it works beatifully. However, when a few words within the
paragraph are selected, the changes continue beyond the selection until
the end of the document.



Upon further investigation, it seems that the Find/Replace feature in
Word (sans any VBA scripting) displays this same (undesired) behavior:

1. Select a single word in a paragraph.
2. In the Find What box, add the "Default Paragraph Font" style
3. In the Replace With box, add the "New/Changed" style
4. Click Replace All
5. All instances of Default Paragraph Font in the entire document are
replaced.

I'm still not sure how to proceed. Surely there's a way to use VBA to
isolate *only* the selection.

Again, any suggestions are appreciated.

Paul Burton
Replies Reply to this message
#2 Stefan Blom
December 23rd, 2011 - 11:38 am ET | Report spam
The Default Paragraph Font "style" is a special style in Word: it represents
the default font formatting of the underlying *paragraph* style. In other
words, if you use it for "Find what," *all* text will be found. (However,
you can use it for "Replace with"; it will then remove direct formatting
from the found text.)

Stefan Blom
Microsoft Word MVP





"Paul" wrote in message news:

On 2011-12-23, Paul wrote:
I've created a macro in Word 2007 that is designed to apply special
formatting to the current selection. When an entire paragraph is
selected, it works beatifully. However, when a few words within the
paragraph are selected, the changes continue beyond the selection until
the end of the document.



Upon further investigation, it seems that the Find/Replace feature in
Word (sans any VBA scripting) displays this same (undesired) behavior:

1. Select a single word in a paragraph.
2. In the Find What box, add the "Default Paragraph Font" style
3. In the Replace With box, add the "New/Changed" style
4. Click Replace All
5. All instances of Default Paragraph Font in the entire document are
replaced.

I'm still not sure how to proceed. Surely there's a way to use VBA to
isolate *only* the selection.

Again, any suggestions are appreciated.

Paul Burton
Replies Reply to this message
#3 Paul
December 23rd, 2011 - 12:43 pm ET | Report spam
On 2011-12-23, Stefan Blom wrote:
The Default Paragraph Font "style" is a special style in Word: it represents
the default font formatting of the underlying *paragraph* style. In other
words, if you use it for "Find what," *all* text will be found. (However,
you can use it for "Replace with"; it will then remove direct formatting
from the found text.)



According to my tests in Word 2007, using Default Paragraph Font in the
Find What box *only* matches text that is tagged with the base paragraph
style. It does not match text that has a character style applied "over"
the paragraph style. In other words the "Find What" portion of my macro
appears to be doing exactly what I want it to. Again, I'm afraid this is
one of the many quirky limitations of Word.

Thanks for your reply.

Paul Burton
Replies Reply to this message
#4 Stefan Blom
December 23rd, 2011 - 01:35 pm ET | Report spam
OK, I misunderstood what you were trying to do... :-(

The style you specified for "Replace with"--New/Revised Text--is a character
style I assume?

Stefan Blom
Microsoft Word MVP





"Paul" wrote in message news:

On 2011-12-23, Stefan Blom wrote:
The Default Paragraph Font "style" is a special style in Word: it
represents
the default font formatting of the underlying *paragraph* style. In other
words, if you use it for "Find what," *all* text will be found. (However,
you can use it for "Replace with"; it will then remove direct formatting
from the found text.)



According to my tests in Word 2007, using Default Paragraph Font in the
Find What box *only* matches text that is tagged with the base paragraph
style. It does not match text that has a character style applied "over"
the paragraph style. In other words the "Find What" portion of my macro
appears to be doing exactly what I want it to. Again, I'm afraid this is
one of the many quirky limitations of Word.

Thanks for your reply.

Paul Burton
Replies Reply to this message
#5 Paul
December 23rd, 2011 - 10:18 pm ET | Report spam
On 2011-12-23, Stefan Blom wrote:
OK, I misunderstood what you were trying to do... :-(

The style you specified for "Replace with"--New/Revised Text--is a character
style I assume?



Yes.
Paul Burton
Replies Reply to this message
Help Create a new topicNext page Replies Make a reply
Search Make your own search