Link to home
Start Free TrialLog in
Avatar of Varun Walia
Varun WaliaFlag for India

asked on

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

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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Varun Walia

ASKER

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,
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.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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..
Thanks a lot Graham skan,,

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

Thanks once again.