Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7032639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:57:41+00:00 2026-05-28T00:57:41+00:00

I’m working on a Word 2007 template with a macro that will apply character

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:57:42+00:00Added an answer on May 28, 2026 at 12:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.