创建和管理样式的能力是Word的强大功能之一。样式使您可以快速,轻松地在整个文档中应用一致的格式,并根据需求的变化更新该格式。

Word提供了很多内置(预定义)样式,您可以根据需要添加更多样式。在某些时候,您可能想确定文档中未使用哪些样式。然后,可以使用此列表来确定您可以轻松删除的样式,这仅仅是因为不再需要它们。

没有在Word中创建未使用的样式列表的内在方法。

相反,您需要创建一个宏来为您完成这项工作。您可能会认为,创建这样的宏将是一项简单的任务,即查看Word相信使用的样式,然后将其与定义的样式进行比较。这种方法的问题在于,VBA的InUse属性(适用于Style对象)用于多种目的。 InUse属性的正式定义是,如果满足以下两个条件之一,则为True:

  • 样式是已在文档中修改或应用的内置样式。

  • 样式是在文档中创建的用户定义样式。

这意味着InUse属性不会指示文档中是否实际使用了样式。您可以在不实际应用样式的情况下对样式进行定义,即使该文档中没有任何实际使用该样式的文本,该样式也会被标记为“正在使用”。

但是,可以通过在宏中同时使用InBuilt和InUse属性来生成未使用的样式列表。下面的VBA宏使用这种方法:

Sub CreateStyleList()

Dim docThis As Document     Dim styItem As Style     Dim sBuiltIn(499) As String     Dim iStyBICount As Integer     Dim sUserDef(499) As String     Dim iStyUDCount As Integer     Dim sInUse(499) As String     Dim iStyIUCount As Integer     Dim iParCount As Integer     Dim J As Integer, K As Integer     Dim sParStyle As String     Dim bInUse As Boolean          ' Ref the active document     Set docThis = ActiveDocument          ' Collect all styles being used     iStyIUCount = 0     iParCount = docThis.Paragraphs.Count     iParOut = 0     For J = 1 To iParCount         sParStyle = docThis.Paragraphs(J).Style         For K = 1 To iStyIUCount             If sParStyle = sInUse(K) Then Exit For         Next K         If K = iStyIUCount + 1 Then             iStyIUCount = K             sInUse(iStyIUCount) = sParStyle         End If     Next J          iStyBICount = 0     iStyUDCount = 0     ' Check out styles that are "in use"

For Each styItem In docThis.Styles         'see if in those being used         bInUse = False         For J = 1 To iStyIUCount             If styItem.NameLocal = sInUse(J) Then bInUse = True         Next J         'Add to those not in use         If Not bInUse Then             If styItem.BuiltIn Then                 iStyBICount = iStyBICount + 1                 sBuiltIn(iStyBICount) = styItem.NameLocal             Else                 iStyUDCount = iStyUDCount + 1                 sUserDef(iStyUDCount) = styItem.NameLocal             End If         End If     Next styItem          'Now create the output document     Documents.Add          Selection.TypeText "Styles In Use"

Selection.TypeParagraph     For J = 1 To iStyIUCount         Selection.TypeText sInUse(J)

Selection.TypeParagraph     Next J     Selection.TypeParagraph     Selection.TypeParagraph          Selection.TypeText "Built-in Styles Not In Use"

Selection.TypeParagraph     For J = 1 To iStyIUCount         Selection.TypeText sBuiltIn(J)

Selection.TypeParagraph     Next J     Selection.TypeParagraph     Selection.TypeParagraph          Selection.TypeText "User-defined Styles Not In Use"

Selection.TypeParagraph     For J = 1 To iStyIUCount         Selection.TypeText sUserDef(J)

Selection.TypeParagraph     Next J     Selection.TypeParagraph     Selection.TypeParagraph End Sub

宏首先检查文档中的每个段落,以确定文档中实际使用的样式的名称。此信息存储在sInUse数组中。然后,宏开始浏览Word认为正在使用的列表-这些是属于“样式”集合的样式。如果样式不在sInUse数组中,则将其添加到sBuiltIn数组(对于内置样式)或sUserDef数组(对于用户定义的样式)中。完成比较后,将创建一个列出结果的新文档。

注意:

如果您想知道如何使用此页面(或_WordTips_网站上的任何其他页面)中描述的宏,我准备了一个包含有用信息的特殊页面。

_WordTips_是您进行经济有效的Microsoft Word培训的来源。

(Microsoft Word是世界上最流行的文字处理软件。)本技巧(1488)适用于Microsoft Word 97、2000、2002和2003。