Find/Replace macro only limited to selection when entire paragraphis selected

P

Paul

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.
 
P

Paul

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.
 
S

Stefan Blom

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
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.
 
P

Paul

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.
 
S

Stefan Blom

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
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.
 
S

Stefan Blom

Well, that's what I thought. If not, it would have been too easy. :)

Happy holidays

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
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.
 
S

Stefan Blom

Does it make a difference if you create a range object from the selection?

Dim r As Range
Set r = Selection.Range

(and so forth)

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
Well, that's what I thought. If not, it would have been too easy. :)

Yeah, that's what I was afraind of. I guess this will be one of those
"It works well enough" kind of things.
 
P

Paul

Does it make a difference if you create a range object from the selection?
Dim r As Range
Set r = Selection.Range
(and so forth)


Yes, using a range does the trick. I had a feeling the answer would lie
somewhere in the Range object, but being relatively inexperienced with
VBA, I wasn't sure and the documentation is a little obtuse. I knew I
would find the experienced help I needed here. Thanks a million!
 
S

Stefan Blom

I'm glad you got it sorted!

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
Does it make a difference if you create a range object from the selection?
Dim r As Range
Set r = Selection.Range
(and so forth)


Yes, using a range does the trick. I had a feeling the answer would lie
somewhere in the Range object, but being relatively inexperienced with
VBA, I wasn't sure and the documentation is a little obtuse. I knew I
would find the experienced help I needed here. Thanks a million!
 
P

Paul

I'm glad you got it sorted!

I thought I did. I swear it worked perfectly when I first used the Range
object. Now even using the range it's behaving exactly as it did before
(I even tried using a backup copy of the template that I created right
after it worked for me).

Back to square one I suppose. Even so, thanks for your help.
 
S

Stefan Blom

If you posted the modified code, maybe someone could suggest improvements.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
I'm glad you got it sorted!

I thought I did. I swear it worked perfectly when I first used the Range
object. Now even using the range it's behaving exactly as it did before
(I even tried using a backup copy of the template that I created right
after it worked for me).

Back to square one I suppose. Even so, thanks for your help.
 
P

Paul

If you posted the modified code, maybe someone could suggest improvements.

Of course. Here's the code:

Sub restyleSelection()
Dim r As Range
Set r = Selection.Range

r.Find.Style = ActiveDocument.Styles("Default Paragraph Font")
r.Find.Replacement.Style = ActiveDocument.Styles("Emphasis")
With r.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
r.Find.Execute Replace:=wdReplaceAll
End Sub

I created a test version of the macro that uses buitin Word styles. If you
create a new document and add a few paragraphs of dummy text, then select
a few words in a paragraph and run the macro, you'll see that the Emphasis
character style is applied beyond the ending boundary of the range and to
the end of the document.

FWIW, I also tested this with the Find/Replace feature itself and got the
same results. Although Find/Replace operates on the selection by default
for most things, this swapping of styles applies beyond the selection.
This leads me to believe I may need to look for another solution beyond
the Find/Replace feature.
 
S

Stefan Blom

I can certainly confirm that it isn't working. Even worse, in Word 2010 it
doesn't work if I make that same find and replace operation in the *user
interface* either. You can tell because the selection is lost after the
macro has run, and the insertion point is at the beginning of the document.
This does not happen when I find and replace (for example) one character
style with another character style in the current selection.

It does seem like a bug. The question is if the bug can be reproduced only
if the Default Paragraph Font is involved, or if there are other cases as
well.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
If you posted the modified code, maybe someone could suggest improvements.

Of course. Here's the code:

Sub restyleSelection()
Dim r As Range
Set r = Selection.Range

r.Find.Style = ActiveDocument.Styles("Default Paragraph Font")
r.Find.Replacement.Style = ActiveDocument.Styles("Emphasis")
With r.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
r.Find.Execute Replace:=wdReplaceAll
End Sub

I created a test version of the macro that uses buitin Word styles. If you
create a new document and add a few paragraphs of dummy text, then select
a few words in a paragraph and run the macro, you'll see that the Emphasis
character style is applied beyond the ending boundary of the range and to
the end of the document.

FWIW, I also tested this with the Find/Replace feature itself and got the
same results. Although Find/Replace operates on the selection by default
for most things, this swapping of styles applies beyond the selection.
This leads me to believe I may need to look for another solution beyond
the Find/Replace feature.
 
P

Paul

It does seem like a bug. The question is if the bug can be reproduced only
if the Default Paragraph Font is involved, or if there are other cases as
well.

It doesn't seem to matter whether the Default Paragraph Font is
involved. Using any combination of character styles seems to achieve
the same result.

So, assuming this is an insurmountable obstacle to strictly using the
Find/Replace API, are there any suggestions for another solution? I'm
trying to think of a way to somehow isolate the individual instances of
each character style in a range/selection and updating the Style
property of the "sub-range". Does that sound viable? If so, any suggestions
on how to go about the "isolate the individual instances" part?
 
S

Stefan Blom

In my tests, replacing one specific character with another in the selection
does seem to work.

As far as alternative methods are concerned, you could use the Characters
collection, which consists of the smallest available range objects:
individual characters (in the main body of the document). You would then
have to look at the properties, including character styles, for each of
those range objects. This is likely to be a slow process, though.

Maybe someone else can figure out a better fix? Keeping fingers crossed...

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
It does seem like a bug. The question is if the bug can be reproduced only
if the Default Paragraph Font is involved, or if there are other cases as
well.

It doesn't seem to matter whether the Default Paragraph Font is
involved. Using any combination of character styles seems to achieve
the same result.

So, assuming this is an insurmountable obstacle to strictly using the
Find/Replace API, are there any suggestions for another solution? I'm
trying to think of a way to somehow isolate the individual instances of
each character style in a range/selection and updating the Style
property of the "sub-range". Does that sound viable? If so, any suggestions
on how to go about the "isolate the individual instances" part?
 
S

Stefan Blom

"Stefan Blom" wrote in message
In my tests, replacing one specific character with another in the
selection does seem to work.

I meant to write character *style* here of course.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
It does seem like a bug. The question is if the bug can be reproduced only
if the Default Paragraph Font is involved, or if there are other cases as
well.

It doesn't seem to matter whether the Default Paragraph Font is
involved. Using any combination of character styles seems to achieve
the same result.

So, assuming this is an insurmountable obstacle to strictly using the
Find/Replace API, are there any suggestions for another solution? I'm
trying to think of a way to somehow isolate the individual instances of
each character style in a range/selection and updating the Style
property of the "sub-range". Does that sound viable? If so, any suggestions
on how to go about the "isolate the individual instances" part?
 
P

Paul

In my tests, replacing one specific character with another in the selection
does seem to work.
As far as alternative methods are concerned, you could use the Characters
collection, which consists of the smallest available range objects:
individual characters (in the main body of the document). You would then
have to look at the properties, including character styles, for each of
those range objects. This is likely to be a slow process, though.

I'll take a look. I think in this case the benefit may outweigh the
extra time it takes to iterate through the selection
character-by-character. Especially since I think in general the
selections will be relatively small.
Maybe someone else can figure out a better fix? Keeping fingers crossed...

Here's hoping. Thanks for all your help.
 
S

Stefan Blom

To clarify, there is a Characters collection for the whole document, and it
includes the characters, paragraph marks, etc. (as range objects) in the
main body of the document. But there are also Characters collections for
selections, or for range objects.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
In my tests, replacing one specific character with another in the
selection
does seem to work.
As far as alternative methods are concerned, you could use the Characters
collection, which consists of the smallest available range objects:
individual characters (in the main body of the document). You would then
have to look at the properties, including character styles, for each of
those range objects. This is likely to be a slow process, though.

I'll take a look. I think in this case the benefit may outweigh the
extra time it takes to iterate through the selection
character-by-character. Especially since I think in general the
selections will be relatively small.
Maybe someone else can figure out a better fix? Keeping fingers crossed...

Here's hoping. Thanks for all your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top