We help IT Professionals succeed at work.

How to check if character style is applied on certain text.

Last Modified: 2018-11-04
Hi All,

Is this possible to check if any character style is applied on some specific text using vba macro?

For eg:- We are here in Expert (1) Exchange.
This is used to provide (2) help by experts.

Here, we have to check, if number in braces (i.e. (1) and (2)) having any character style, it will pop up an alert.

Watch Question

Top Expert 2012

I think you need something like this:
Sub StyleCheck()
    Dim rng As Range
    Dim rng1 As Range
    Dim sty As Style
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "\([0-9]{1,}\)"
        .MatchWildcards = True
        Do While .Execute
            Set rng1 = rng.Duplicate
            rng1.MoveStart wdCharacter, 1
            rng1.MoveEnd wdCharacter, -1
            Set sty = rng1.Style
            If sty.Type = wdStyleTypeCharacter Then
                MsgBox "Text at position: " & rng1.Start & " has character style: " & sty.NameLocal
            End If
    End With

Open in new window

End Sub
Varun WaliaPHP developer


Exactly Skanian.....

Facing one more issue in the same.... I tried but no solution..
If any space found in the braces?
For eg. ([space/tab]1) or (2[space]) or (2[space]3)
We need to eliminate the spaces in the bracket from everywhere.

I used trimming in this, but no response.

Help me out.

Varun WaliaPHP developer


Hi GrahamSkan,

one more thing, what if I am getting character style one part also, means....
this may be ( and 1 and )

than also it should raise an alert..

I think three regex are needed, if i am not wrong.
Top Expert 2012
This one is on us!
(Get your first solution completely free - no credit card required)
Varun WaliaPHP developer


Thanks skan..

You did it..

Should I run separate macro to find bold, Italic, bold-Italic on the same text.

If possible please share changed code..

Thanks a ton anyways..
Varun WaliaPHP developer


Thanks a lot Graham skan,,

Please check my last comment and make my output perfect. As you always do.

Thanks once again.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.