0

MS-Word ドキュメント内の特定の単語を見つけるために RegEx 検索を使用しています。検索結果は変数に格納されます。私の問題は、検索結果だけにカスタム スタイルを適用したいことです。

入力: 世界中[1,2]。[1,3,4][1,2,4,5] [1,2,6,7,8] [1,2] [1,2] の前、最中、または後

次のコードを使用しています

Sub RegexReplaces()
    Set matches = New regExp
    Dim Sure As Integer
    Dim rng As Range
    matches.Pattern = "([\[\(][0-9, -]*[\)\]])"
    matches.Global = True
    Dim mat As MatchCollection
    Set mat = matches.Execute(ActiveDocument.Range)
    For Each m In mat
        Sure = MsgBox("Are you sure?" + m, vbOKCancel)
        If Sure = 1 Then
            m.Style = ActiveDocument.Styles("Heading 1")    'this is the error line
        Else
            MsgBox "not1111"
        End If
    Next m
End Sub
4

1 に答える 1

1

For Each m In matループは、マット コレクション内の各アイテムを反復処理します。M は範囲ではありません。m.FirstIndex で始まり、m.FirstIndex + m.Length で終わる範囲を設定する必要があります。次に、範囲を選択し、Selection.Style を使用して範囲のスタイルを設定する必要があります。

Sub RegexReplaces()
    Set matches = New regExp
    Dim Sure As Integer
    Dim rng As Range
    matches.Pattern = "([\[\(][0-9, -]*[\)\]])"
    matches.Global = True
    Dim mat As MatchCollection
    Set mat = matches.Execute(ActiveDocument.Range)
    For Each m In mat
        Sure = MsgBox("Are you sure?" + m, vbOKCancel)
        If Sure = 1 Then

            Set rng = ActiveDocument.Range(Start:=m.FirstIndex, End:=m.Length + m.FirstIndex)
            rng.Select
            Selection.Style = ActiveDocument.Styles("Heading 1")
        Else
            MsgBox "not1111"
        End If
    Next m
End Sub
于 2016-08-04T08:31:13.383 に答える