Dim Reg As New RegExp, Matches, Match
Dim myselection As Word.Selection
Set myselection = Documents.Application.Selection
With Reg
.Global = True
.IgnoreCase = True
.Pattern = "<.*?>"
.MultiLine = True
Set Matches = .Execute(myselection)
For Each Match In Matches
MsgBox Match
Reg = .Replace(Match, "")
Next
End With
請問上面 Reg = .Replace(Match, "") 寫錯了嗎,為什么能夠找到匹配物件,卻無法把匹配物件替換為空,即去掉呢?或者大家有更好的辦法能夠把選定文本中 <.*?> 這樣規則的東西去除
uj5u.com熱心網友回復:
Reg = .Replace(Match, "") ?物件等于一字串,不對吧? 應該是這樣子:Text2 = Reg.Replace(Text1,"")uj5u.com熱心網友回復:
請參考:vba中如何對word指定范圍中的字符,使用通配符(正則運算式)進行替換?
uj5u.com熱心網友回復:
既然已經設定 .Global = True 就用不著 For 回圈。試試,把你的 Selection 轉換成字串:
Reg.Replace(myString, "")
然后,將字串替換掉也來 Selection 的內容。
或者干脆用 Word 的宏來實作替換。
uj5u.com熱心網友回復:
Replace 不需要先呼叫 Execute。Dim Reg As New RegExp, Matches, Match
Dim myselection As Word.Selection
Set myselection = Documents.Application.Selection
With Reg
.Global = True
.IgnoreCase = True
.Pattern = "<.*?>"
.MultiLine = True
MsgBox .Replace(myselection, "")
End With
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/95277.html
標籤:控件
