简体   繁体   English

VBA 如何读取标题样式枚举?

[英]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.这解决了问题,但问题是如果有人以不同的语言运行 Word,“标题 1”不再适用。 I tried with wdstyleheading1 as well, but the p.Style always reads out the actual name of the style, not an enumeration.我也尝试使用wdstyleheading1 ,但是p.Style总是读出样式的实际名称,而不是枚举。

I thought of p.Style.ListLevelNumber , but that seems to be too vague, and might mess up other lists in the document.我想到了p.Style.ListLevelNumber ,但这似乎太模糊了,可能会弄乱文档中的其他列表。

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.您可以使用.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.这是一种解决方法,因为我认为您实际上无法将aparagraphs.Style 与枚举进行比较,但这应该可以解决问题。

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:否则,对所有 9 个标题 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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