根据允许的样式列表检查Word中的样式名称

时间:2017-11-24 12:13:36

标签: vba ms-word styles names

我的知识库中有4,000条记录,几乎都是Word格式的记录。许多格式化为我们的预设样式,其中大约有20个。现在我们正在实现新的样式名称:(我想写一个宏来查看批量记录,更改'样式A'到了新的A','样式B'新的B'等等我可以这样做但是想问 如何检查段落的当前样式名称是否是我们的20个预设样式之一 。例如,如果样式名称是标准的Word样式,或者是从中拉出的样式在其他地方,我想做一些其他事情,而不是将其转换为新风格。非常感谢!

2 个答案:

答案 0 :(得分:0)

要查找所选段落的样式名称,请使用:

Selection.ParagraphFormat.Style.NameLocal  ' or Name

要处理内置样式,请使用:

ActiveDocument.Styles(wdStyleNormal)

wdStyleNormal内置Word常量,其中包含许多(大多数)标准样式。我可以使用样式名称来处理您自己的(非内置)样式。查找Styles以获取详细信息。

以下用于将样式从准备好的模板分发(复制)到用户的Normal模板的宏:

Sub CopyStyles()
Dim n, src, dst
    '
On Error Resume Next
    src = ActiveDocument.AttachedTemplate.FullName
    dst = NormalTemplate.FullName
    '
    ' Copy styles
    '
    n = ActiveDocument.Styles(wdStyleNormal).ParagraphFormat.SpaceBefore
    ActiveDocument.Styles(wdStyleNormal).ParagraphFormat.SpaceBefore = 0
        Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleNormal).NameLocal
    ActiveDocument.Styles(wdStyleNormal).ParagraphFormat.SpaceBefore = n
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleHeading1).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleHeading2).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleHeading3).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleHeading4).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleHeading5).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleHeading6).NameLocal

    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleTOC1).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleTOC2).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleTOC3).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleTOC4).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleTOC5).NameLocal
    Application.OrganizerCopy Source:=src, Destination:=dst, Object:=wdOrganizerObjectStyles, Name:=ActiveDocument.Styles(wdStyleTOC6).NameLocal

End Sub

答案 1 :(得分:0)

也许我明白你的问题是对的。以下是一些可能更符合您所寻找方向的示例:

Sub test()
Dim para As Paragraph

    ' rename a style of the document
    '
    On Error GoTo lbl_NoSuchStyle
        ActiveDocument.Styles("TestStyle1").NameLocal = "TestStyle2"
    On Error GoTo 0
lbl_NoSuchStyle:
    '
    ' Check styles of all paragraphs
    '
    For Each para In ActiveDocument.Paragraphs
        Select Case para.Format.Style.NameLocal
            Case "Req"
                Debug.Print "Isa Req"
            Case "TestStyle2"
                para.Style = wdStyleHeading3  ' change style to another style
            Case Else
                Debug.Print "Unknown Style"
        End Select
    Next para

End Sub