0

我想用新字符串替换所有三个字母的单词。MsgBox 显示变量threeLetterWord 有新值,但文本不变。

Sub ShowThreeLetterWords()

Set threeLetterRegExp = New RegExp
   threeLetterRegExp.Pattern = "\b[a-zA-Z]{3}\b"
   threeLetterRegExp.Global = True

   Dim threeLetterWords As MatchCollection
   Set threeLetterWords = threeLetterRegExp.Execute(ActiveDocument.Range)

   For Each threeLetterWord In threeLetterWords
      threeLetterWord = threeLetterRegExp.Replace(threeLetterWord, "sasa")
      MsgBox threeLetterWord
   Next threeLetterWord
 End Sub
4

1 回答 1

1

正则表达式很好。我认为唯一的问题是你不应该在循环中再次分配 treeLetterWord,因为它是一个“foreach 迭代变量”。

直接地

MsgBox threeLetterRegExp.Replace(threeLetterWord, "sasa")

应该可以

于 2012-11-30T01:58:04.463 回答