Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace a Microsoft Word character style within a range/selection in VBA?

Tags:

ms-word

vba

I'm working on a Word 2007 template with a macro that will apply character styles to the selected text. It seemed that the Find/Replace feature would be a good place to start, but I think I've found a bug/limitation that prevents the macro from working as desired.

Here's my vba code:

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

If I create a test document that contains a few paragraphs and select a few words in one of the paragraphs, then run the macro, the "Emphasis" style is applied not only to the selection, but beyond the end of the selection to the end of the document.

This behavior is the same using the actual GUI Find/Replace tool.

My question is: How can I overcome this bug/limitation and apply the character style ONLY within the selection/range?

A little more information:
What I really need the macro to do is apply certain formatting to the entire selection while maintaining the existing character styles in the selection. For example, if the selected text contains the Bold character style, the Italic character style, and the rest of it is Default Paragraph Font, the macro should replace Bold with "Revised Bold", replace "Italic" with "Revised Italic", and replace "Default Paragraph Font" with "Revised". That way, when I use the companion macro to "undo" the action of this macro, the original character styles (Bold, Italic, Default Paragraph Font) can be replaced.

SOLVED:
Here is the solution I finally arrived at:

Sub applyNewRevisedText
    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 "Bold"
            rng.Style = ActiveDocument.Styles("New/Revised Text Bold")
        Case "Italic"
            rng.Style = ActiveDocument.Styles("New/Revised Text Emphasis")
        Case Else
            rng.Style = ActiveDocument.Styles("New/Revised Text")
        End Select
    Next rng
End Sub
like image 573
PaulBurton0 Avatar asked Nov 05 '22 08:11

PaulBurton0


1 Answers

To answer your direct question

My question is: How can I overcome this bug/limitation and apply the character style ONLY within the selection/range?

Does this not meet the need?:

Sub restyleSelection()
    Selection.Style = ActiveDocument.Styles("Emphasis")
End Sub

EDIT:

Ok, based on your comment, what about something like:

Dim rng As Range
  For Each rng In Selection.Words
    If rng.Bold 'do something
  Next rng

.Words will break up each word in the range into a collection of ranges. Then you can perform styling on each individual word based on its current style.

like image 94
Justin Self Avatar answered Nov 15 '22 06:11

Justin Self