-2
VBAマクロでRegExを使用して、検索機能が不十分な場合があるため、ワードファイル内で一致するものを見つけることができます。 マッチはマークされていなければなりません(作業中)、コメントを適用する必要があります。 しかし、コメント(またはコメントが既に存在する)を追加した場合、Match.FirstIndexは前回のコメントごとに1つずつスローされます。 同じテキストが1つと数えるコメント。RegExとコメントを使用したWord VBA
なぜですか?それを修正するには?
簡単な例:
Sub Mark_QuestionAndExpressionMarks()
Dim Match As Match
Dim Matches
Dim regEx As New regExp
regEx.Pattern = "\?|!" 'regex for questionmark or expressionmark
regEx.IgnoreCase = False
regEx.Global = True
Set Matches = regEx.Execute(ActiveDocument.Content.Text) ' or ActiveDocument.Content.Text
For Each Match In Matches
Call HighlightAndComment(ActiveDocument.range(Match.FirstIndex, Match.FirstIndex + Len(Match.Value)), "Question or Expressionmark")
// problem here as Match is realized correctly but the FirstIndex is off
Next
End Sub
Sub HighlightAndComment(WordOrSentence As Object, comment As String)
WordOrSentence.HighlightColorIndex = wdYellow
Call ActiveDocument.range.Comments.Add(WordOrSentence, comment)
End Sub
単語またはExcelですか?それが両方の場合、どのように関係していますか? – Deduplicator
Wordのみ、いいえexcel – Nandmp
ハイライトは '?'や '!'の前に2番目以降の文字でハイライトが設定されているのですか?一致FirstIndexが正しく計算されるので、これは本当に奇妙です。 –