locked
WdBuiltinStyle constants not recognized in an If clause RRS feed

  • Question

  • Hi,

    I'm trying to write a macro to process all headings of a document, looking for headings with the WdBuiltinStyle enumaration (For Each oParagraph in ActiveDocument.Paragraphs ... Next oParagraph). What I realized is that the following clause will never be True:

    If oParagraph.Style = wdStyleHeading1 Or oParagraph.Style = wdStyleHeading2 Or oParagraph.Style = wdStyleHeading3

    But this will work:

    If oParagraph.Style = "Heading 1" Or oParagraph.Style = "Heading 2" Or oParagraph.Style = "Heading 3"

    On the other hand, I can use use these constants to set a style:

    oParagraph.Style = wdStyleHeading4

    Obviously, I'm missing something on the proper use of these constants (it doesn't work to get a style, but it does to set a style. Could someone please help me understand?

    Thanks!

    R.

    Wednesday, September 17, 2014 7:21 AM

Answers

  • Although you can apply a style with an integer that relates to a builtin style reading the default property returned reading the style is its LocalName, and there isn't a property that returns an index if its also builtin style. However this worked for me 

    Sub test()
    Dim doc As Document
    Dim oParagraph As Paragraph
    Dim a(1 To 3) As String
    
        Set doc = ActiveDocument
        a(1) = doc.Styles(wdStyleHeading1).NameLocal
        a(2) = doc.Styles(wdStyleHeading2).NameLocal
        a(3) = doc.Styles(wdStyleHeading3).NameLocal
    
        For Each oParagraph In doc.Paragraphs
            Select Case oParagraph.Style
            Case a(1), a(2), a(3)
                Debug.Print "Heading 1 to 3:", oParagraph.Style
            Case Else:
                Debug.Print "other style", oParagraph.Style
            End Select
        Next oParagraph
    
    End Sub

    • Marked as answer by Mouse Art Wednesday, September 17, 2014 2:22 PM
    Wednesday, September 17, 2014 9:42 AM

All replies

  • Although you can apply a style with an integer that relates to a builtin style reading the default property returned reading the style is its LocalName, and there isn't a property that returns an index if its also builtin style. However this worked for me 

    Sub test()
    Dim doc As Document
    Dim oParagraph As Paragraph
    Dim a(1 To 3) As String
    
        Set doc = ActiveDocument
        a(1) = doc.Styles(wdStyleHeading1).NameLocal
        a(2) = doc.Styles(wdStyleHeading2).NameLocal
        a(3) = doc.Styles(wdStyleHeading3).NameLocal
    
        For Each oParagraph In doc.Paragraphs
            Select Case oParagraph.Style
            Case a(1), a(2), a(3)
                Debug.Print "Heading 1 to 3:", oParagraph.Style
            Case Else:
                Debug.Print "other style", oParagraph.Style
            End Select
        Next oParagraph
    
    End Sub

    • Marked as answer by Mouse Art Wednesday, September 17, 2014 2:22 PM
    Wednesday, September 17, 2014 9:42 AM
  • OK, that's clear to me now! :-)

    Thanks!

    Wednesday, September 17, 2014 2:22 PM