0

スタイル ウィンドウ内で、スタイルのプルダウン メニューをクリックし、スタイルがドキュメント内の任意の場所に割り当てられると、そのスタイルのインスタンス数がすぐに表示され、すべてのインスタンスを選択または削除する可能性が提供されます。 . スタイルがどこにも割り当てられていない場合、これらのオプションはグレー表示されます。

VBA プログラム内でこの情報が必要ですが、それを取得する便利な方法が見つかりませんでした。

回避策として、 Tech-Tav.comの「DeleteUnusedStyles」マクロを変更しましたが、私のコードは非常に遅いため、ca 350 組み込みの Microsoft スタイルの処理は実用的ではありません。特定のスタイルが割り当てられたインスタンスは数えませんが、それが割り当てられたストーリー範囲内だけです。このように個々のインスタンスをカウントすると、マクロの実行時間が 1 時間以上になります。

VBA 内から必要な情報を直接取得する方法がない場合 (そして、私は 1 週間以上無駄に調査しました)、誰かが検索ルーチンを高速化する方法のヒントを教えてくれませんか?

ルーチンには次の情報が必要です。

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
4

1 に答える 1

0

Style.InUse プロパティを使用して、スタイルが使用されているかどうかをすばやく見つけてから、Find を使用してインスタンス数を取得できると思います。

于 2013-03-24T13:46:40.307 に答える