2016-08-04 8 views
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 

答えて

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 
+0

私は本当にこれを行う方法について心配していたので、お返事ありがとうございました。この答えは私には大いに意味します。 – Kevin

+0

私は助けることができてうれしいです!私の答えを受け入れてくれてありがとう。 –

関連する問題