简体   繁体   中英

VBA how to read heading style enumeration?

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?

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

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.

Actually, if your style definitions are correct in the attached template, you don't even need a macro for this. All you need do is check the 'automatically update document styles' option, found under Developer|Document Template>Templates. With that set, all you need do is save, close, then re-open the document.

Otherwise, using Find/Replace for all 9 Heading Styles:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  'Style enumerations run from -2 (wdStyleHeading1) to -10 (wdStyleHeading9)
  For i = -2 To -10 Step -1
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = ""
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = True
      .Style = i
      .Replacement.Style = i
      .Execute Replace:=wdReplaceAll
    End With
    Do While .Find.Execute
      .Paragraphs(1).Range.Style = i
      If .Information(wdWithInTable) = True Then
        If .End = .Cells(1).Range.End - 1 Then
          .End = .Cells(1).Range.End
          .Collapse wdCollapseEnd
          If .Information(wdAtEndOfRowMarker) = True Then .End = .End + 1
        End If
      End If
      If .End = ActiveDocument.Range.End Then Exit Do
      .Collapse wdCollapseEnd
    Loop
  Next
End With
Application.ScreenUpdating = True
MsgBox "Done."
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM