We help IT Professionals succeed at work.

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

83 Views
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.

Thanks
Comment
Watch Question

GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
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
        Loop
    End With

Open in new window

End Sub
Varun WaliaPHP developer

Author

Commented:
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.

Thanks,
Varun WaliaPHP developer

Author

Commented:
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.
Thanks.
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Varun WaliaPHP developer

Author

Commented:
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

Author

Commented:
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.

OR

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.