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

asked on

How to check "para" & "character" style, in same paragraph?

How to check a paragraph that it contain both paragraph style and character style in it.

Sample document is attached herewith.

We have to check that paragraph style "ref" and  character style is "Volume" exist in the document using VBA macro.

Thanks
CH05_CE.doc
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

See if I can do it without any silly mistakes:
Sub CheckCharInParaStyle()
    Dim docStyle As Document
    Dim rngPara As Range
    Dim strText As String
    Dim strCharStyle As String
    Dim strParaStyle As String
    Dim para As Paragraph
    
    strParaStyle = "ref"
    strCharStyle = "Volume"
    
    
    Set docStyle = Documents.Open("C:\MyFolder\" & "CH05_CE.doc")
    Set rngPara = docStyle.Range
    
    With rngPara.Find
        .Style = strParaStyle
        Do While .Execute()
            Set para = rngPara.Paragraphs.First
            With para.Range.Find
                .Style = strCharStyle
                If .Execute Then
                    MsgBox "Document has a paragraph with style :'" & strParaStyle & _
                           "', where some of its text has the character style: '" & strCharStyle & "'"
                    Exit Sub
                End If
            End With
        Loop
    End With
    MsgBox "Document is without a paragraph with style :'" & strParaStyle & _
           "', where some of its text has the character style: '" & strCharStyle & "'"
End Sub

Open in new window

Avatar of Varun Walia

ASKER

I try something like this to check character style in ref paragraph styles. Please suggest!!

Sub FindNewCharStyles()

    Dim docActive As Document
    Dim docNew As Document
    Dim styleLoop As Style
    Dim StyleArr() As String
    
    refCharArr(1) = "First page"
    refCharArr(2) = "Last page"
    refCharArr(3) = "Default Paragraph Font"
    
    i = 1
   
    Set docActive = ActiveDocument
    
    
    For Each styleLoop In docActive.Styles
       If (styleLoop.InUse = True And styleLoop.Type = wdStyleTypeCharacter) Then
       If (Not inRef(styleLoop.NameLocal)) Then
            ReDim Preserve StyleArr(i)
            StyleArr(i) = styleLoop.NameLocal
            i = i + 1
        End If
        End If
    Next styleLoop
    
    For J = 1 To UBound(StyleArr)
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.HomeKey Unit:=wdStory
    
    With Selection.Find
        .Text = ""
        .Style = StyleArr(J)
         While .Execute()
            pStyle = Selection.Range.Paragraphs(1).Style
            flag = False
            If pStyle = "ref" Then
             Fileout.Writeline StyleArr(J) & " is a wrong style!"
             End If
        Wend
    End With
    Next J
    
    
End Sub

Function inRef(rStyle As String)
    Dim flag As Boolean
    flag = False
    For i = 1 To UBound(refCharArr)
        If (refCharArr(i) = rStyle) Then flag = True
    Next i
    
    inRef = flag
End Function

Open in new window

As in your other question, does my macro not work?

I repeat the comment about Selection vs Range objects and repost the link to my article (it is still in my Windows clipboard)
 
https://www.experts-exchange.com/articles/9909/Using-the-Word-Range-object-in-code.html
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.