在样式窗口中,当您单击样式的下拉菜单并且样式已分配到文档中的任何位置时,您会立即看到该样式的实例数,并可以选择或删除所有实例. 如果尚未在任何地方指定样式,则这些选项将显示为灰色。
我在 VBA 程序中需要这些信息,但我还没有找到任何方便的方法来获取它,尽管 WinWord 似乎随时可以在它的指尖下获得它。
作为一种解决方法,我已经修改了来自 Tech-Tav.com 的“DeleteUnusedStyles”-Macro ,但是我的代码太慢了,以至于处理 ca 350 内置 Microsoft 样式变得不切实际 - 就目前而言,宏不甚至不计算已分配某种风格的实例,而只计算已分配的故事范围。以这种方式计算每个单独的实例将使宏运行一个多小时:
如果没有直接的方法从 VBA 中获取所需的信息(我已经徒劳地研究了一个多星期),有人可以给我一个提示如何加快搜索程序吗?
该例程需要以下信息:
Dim iStyle% '(the number of the style which is to be processed)
并交回以下信息:
Dim V_NumberOfStylesFound% '(how many styles have been found within the document)
Dim Dim A_StylesUsedInDoc() As String '(An array containing all styles which have been used as base style)
Dim V_BaseStych leListedWhere '(A string listing the Story ranges whithin which the style has been found)
Sub S_SearchForStylesInDocument(V_NumberOfStylesFound, A_StylesUsedInDoc, iStyle, V_BaseStyleListedWhere)
Dim R_MyRange, R_MyStory As Range
Set O_MyStyle = ActiveDocument.Styles(iStyle)
V_iNameLocal = ActiveDocument.Styles(iStyle).NameLocal
V_iBaseStyle = ActiveDocument.Styles(iStyle).BaseStyle
V_ListedWhere = ""
StatusBar = iStyle & " Examining Story Ranges"
For Each R_MyStory In ActiveDocument.StoryRanges
Set R_MyRange = R_MyStory
'StatusBar = iStyle & " Examining " & F_ResolveStoryName(R_MyStory.StoryType) & "..."
R_MyRange.Find.ClearFormatting
R_MyRange.Find.Style = ActiveDocument.Styles(O_MyStyle)
With R_MyRange.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
If R_MyRange.Find.Execute Then
StatusBar = iStyle & " Examining Styles Found"
Select Case V_NumberOfStylesFound
Case 0
If V_iBaseStyle <> "" And V_iBaseStyle <> V_iNameLocal Then
V_NumberOfStylesFound = V_NumberOfStylesFound + 1
A_StylesUsedInDoc(V_NumberOfStylesFound) = V_iBaseStyle
End If
Case Is > 0
Dim i%, Vb_IsListed As Boolean
Vb_IsListed = False
If Vb_IsListed = False Then 'found style is not yet listed
Dim j%, Vb_IsBaseStyleListed As Boolean
'StatusBar = V_iNameLocal & " style is in use."
For j = 1 To V_NumberOfStylesFound 'check whether the base style to found style is already listed?
If A_StylesUsedInDoc(j) = V_iBaseStyle Then
j = V_NumberOfStylesFound
Vb_IsBaseStyleListed = True
End If
Next j
If Vb_IsBaseStyleListed = False And V_iBaseStyle <> "" Then 'base style to found style is not yet listed
V_NumberOfStylesFound = V_NumberOfStylesFound + 1
A_StylesUsedInDoc(V_NumberOfStylesFound) = V_iBaseStyle
'StatusBar = V_iBaseStyle & " style is in use."
End If
Else
'Stop 'how can style get listed twice?
End If
V_BaseStyleListedWhere = V_ListedWhere & R_MyStory.StoryType & ","
End Select
Set R_MyRange = R_MyStory
End If
Next R_MyStory
'Stop
End Sub