Home > Enterprise >  VBA how to read heading style enumeration?
VBA how to read heading style enumeration?

Time:09-06

I have a code that loops through the paragraphs of a document, and reapplies the heading style. This is in order to fix the numbering inconsistency after copy-pasting headings from different documents.

In my file I just have the following example:

For Each p In ThisDocument.Paragraphs
    If p.Style = "Heading 1" Then
        p.Style = "Heading 1"
    End If
Next p

This solves the issue, but the problem is if somebody has Word running in a different language, "Heading 1" is not applicable anymore. I tried with wdstyleheading1 as well, but the p.Style always reads out the actual name of the style, not an enumeration.

I thought of p.Style.ListLevelNumber, but that seems to be too vague, and might mess up other lists in the document.

Is there a way to read or reference it in a language independent way? Is there perhaps a better solution altogether for the numbering problem?

CodePudding user response:

you can get the local name for a style with .NameLocal. It's kind of a workaround because I don't think you can actually compare a paragraphs .Style with the enum, but this should do the trick.

For Each p In ThisDocument.Paragraphs
    If p.Style = ActiveDocument.Styles(wdStyleHeading1).NameLocal Then
        p.Style = wdStyleHeading1
    End If
Next p

CodePudding user response:

As simple as:

For Each p In ThisDocument.Paragraphs
    If p.Style = wdStyleHeading1 Then
        p.Style = wdStyleHeading1
    End If
Next p

That said, using Find/Replace is way faster than looping through all paragraphs.

  • Related