2013-03-24 12 views
0

在样式窗口中,当您单击样式的下拉菜单并且样式已分配给文档中的任何位置时,您将立即显示该样式的实例数,并且可以选择或删除所有实例。如果样式没有被分配到任何地方,这些选项会变灰。WinWord2007 - 是否有任何直接的VBA访问样式窗口的样式实例计数?

我需要VBA程序内的这些信息,但我还没有找到任何方便的方法来获取它,虽然WinWord似乎有它在指尖下不断提供。

作为解决方法,我修改了“DeleteUnusedStyles”-Macro从Tech-Tav.com,但是我的代码太慢了,处理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 

回答

0

我想你可以使用Style.InUse Property快速找到一个风格是否已被使用,然后找到获得实例数量。

+0

对不起,德语 - 这里又是英语: Style.InUse不通知我一种风格是否已实际应用。在描述中,他们相应地编写了_“InUse属性不一定表明样式是否应用于文档中的任何文本。”_例如,如果我修改内置样式 - 无论是应用还是应用不要将其应用于任何文本 - 它将被标记为“inUse”。似乎没有办法进行风格搜索,然后必须重复所有故事范围。 – Marcel 2013-03-25 12:55:02

+0

好的,但它应该通过消除尚未使用或修改的样式来加快搜索速度。 – grahamj42 2013-03-25 13:02:44

+0

这听起来没错。不幸的是,我的所有样式都被标记为“inUse”,尽管我仅使用了其中的一小部分。所以,检查这个属性不会有什么区别。我必须找到另一种加速循环的方法。 – Marcel 2013-03-26 21:16:53