Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1,787 views
Skip to first unread message

Paul

unread,
Dec 23, 2011, 8:32:45 AM12/23/11
to
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

Paul

unread,
Dec 23, 2011, 10:00:54 AM12/23/11
to
On 2011-12-23, Paul <PaulB...@gmail.com> 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

Stefan Blom

unread,
Dec 23, 2011, 11:38:29 AM12/23/11
to
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:6mhes8x...@soren625.no-ip.org...

Paul

unread,
Dec 23, 2011, 12:43:20 PM12/23/11
to
On 2011-12-23, Stefan Blom <Stefa...@discussions.microsoft.com> 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

Stefan Blom

unread,
Dec 23, 2011, 1:35:47 PM12/23/11
to
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:o6res8x...@soren625.no-ip.org...

Paul

unread,
Dec 23, 2011, 10:18:41 PM12/23/11
to
On 2011-12-23, Stefan Blom <Stefa...@discussions.microsoft.com> 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

Stefan Blom

unread,
Dec 24, 2011, 8:38:36 AM12/24/11
to
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 news:htsfs8x...@soren625.no-ip.org...

Paul

unread,
Dec 24, 2011, 2:16:51 PM12/24/11
to
On 2011-12-24, Stefan Blom <Stefa...@discussions.microsoft.com> wrote:
> 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.

> Happy holidays

Thanks, you too.

--
Paul Burton

Stefan Blom

unread,
Dec 24, 2011, 2:41:41 PM12/24/11
to
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 news:32lhs8x...@soren625.no-ip.org...

Paul

unread,
Jan 3, 2012, 12:45:52 PM1/3/12
to
On 2011-12-24, Stefan Blom <Stefa...@discussions.microsoft.com> wrote:
> 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!

--
Paul Burton

Stefan Blom

unread,
Jan 3, 2012, 8:14:54 PM1/3/12
to
I'm glad you got it sorted!

--
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message news:gfrbt8x...@soren625.no-ip.org...

Paul

unread,
Jan 4, 2012, 2:05:48 PM1/4/12
to
On 2012-01-04, Stefan Blom <Stefa...@discussions.microsoft.com> wrote:
> 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.
--
Paul Burton

Stefan Blom

unread,
Jan 4, 2012, 2:52:19 PM1/4/12
to
If you posted the modified code, maybe someone could suggest improvements.

--
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message news:chket8x...@soren625.no-ip.org...

Paul

unread,
Jan 5, 2012, 9:24:58 AM1/5/12
to
On 2012-01-04, Stefan Blom <Stefa...@discussions.microsoft.com> wrote:
> 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.

--
Paul Burton

Stefan Blom

unread,
Jan 5, 2012, 10:01:50 AM1/5/12
to
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 news:qeogt8x...@soren625.no-ip.org...

Paul

unread,
Jan 5, 2012, 11:15:34 AM1/5/12
to
On 2012-01-05, Stefan Blom <Stefa...@discussions.microsoft.com> wrote:
> 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?
--
Paul Burton

Stefan Blom

unread,
Jan 5, 2012, 5:59:09 PM1/5/12
to
"Stefan Blom" wrote in message news:je59sq$hlr$1...@dont-email.me...

> 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 news:6uugt8x...@soren625.no-ip.org...

Stefan Blom

unread,
Jan 5, 2012, 5:57:27 PM1/5/12
to
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 news:6uugt8x...@soren625.no-ip.org...

Paul

unread,
Jan 6, 2012, 10:01:46 AM1/6/12
to
On 2012-01-05, Stefan Blom <Stefa...@discussions.microsoft.com> wrote:
> 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.

--
Paul Burton

Stefan Blom

unread,
Jan 6, 2012, 10:56:31 AM1/6/12
to
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 news:qvejt8x...@soren625.no-ip.org...

Paul

unread,
Jan 6, 2012, 11:16:03 AM1/6/12
to
On 2012-01-06, Stefan Blom <Stefa...@discussions.microsoft.com> wrote:
> 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.

I see. I'll look into it. Thanks again.
--
Paul Burton

Paul

unread,
Jan 6, 2012, 1:02:11 PM1/6/12
to
OK, with help from here on m.p.w.v.g and from Stack Overflow, I finally
got it working the way I want it to. Instead of stepping through the
selection/range chraracter-wise, I'm doing it word-wise. It's fast
enough and fits the bill quite nicely. Thanks once more for all your
help. Working vba code is below:

Sub applyNewRevisedText(control as IRibbonControl)
Dim r As Range ' Create a new Range object
Set r = Selection.Range ' Assign the current selection to the Range

Dim rng As Range

For Each rng In r.Words

Set rngStyle = rng.Style

Select Case rngStyle
Case "Superscript"
rng.Style = ActiveDocument.Styles("New/Revised Text Superscript")
Case "Subscript"
rng.Style = ActiveDocument.Styles("New/Revised Text Subscript")
Case "Emphasis"
rng.Style = ActiveDocument.Styles("New/Revised Text Emphasis")
Case "Bold"
rng.Style = ActiveDocument.Styles("New/Revised Text Bold")
Case "Italic"
rng.Style = ActiveDocument.Styles("New/Revised Text Italic")
Case "Bold Italic"
rng.Style = ActiveDocument.Styles("New/Revised Text Bold Italic")
Case "Underline"
rng.Style = ActiveDocument.Styles("New/Revised Text Underline")
Case Else
rng.Style = ActiveDocument.Styles("New/Revised Text")
End Select
Next rng
End Sub
--
Paul Burton

Stefan Blom

unread,
Jan 6, 2012, 2:21:00 PM1/6/12
to
Good thinking! Since it's unlikely that you ever want to apply direct
formatting to parts of a word, using the Words collection is more clever
than using Characters, and it should also save some time.

Why didn't I think of that? :-)

Anyway, I'm glad you got it sorted, and thank you for the follow-up.

--
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message news:3ipjt8x...@soren625.no-ip.org...
0 new messages