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

Varun Walia
Varun Walia used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
GrahamSkanRetired
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.
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Retired
Top Expert 2012
Commented:
See if this works with your new  situation:
Sub StyleCheck1()
    Dim rng As Range
    Dim rng1 As Range
    Dim sty As Style
    Dim ch As Range
    Dim Stylecount As Integer
    Dim strPositions As String
    
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "\([0-9 ]{1,}\)"
        .MatchWildcards = True
        Do While .Execute
            Set rng1 = rng.Duplicate
            With rng1.Find
                .Text = " "
                .Replacement.Text = ""
                .Execute Replace:=wdReplaceAll
            End With
            For Each ch In rng1.Characters
                Set sty = ch.Style
                If sty.Type = wdStyleTypeCharacter Then
                    strPositions = strPositions & ", " & ch.Start
                    Stylecount = Stylecount + 1
                End If
            Next ch
        Loop
    End With
    If Stylecount > 0 Then
        strPositions = Mid(strPositions, 3)
        MsgBox "Character style found for " & Stylecount & " characters at positions:" & vbCrLf & _
            strPositions
    Else
        MsgBox "No character styles found in bracketed numbers"
    End If
End Sub

Open in new window

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start Today