vba中怎么对word指定范围中的字符,使用通配符(正则表达式)进行替换
vba中如何对word指定范围中的字符,使用通配符(正则表达式)进行替换?
vba中如何对word指定范围中的字符,使用通配符(正则表达式)进行替换?
下面的代码希望:对tempRange2内“河”“湖”“江”等替换成*水*,都没有效果。
请指教。
谢谢!
------解决思路----------------------
不好意思,word的vba不怎么用,你试试下面的行不行:
------解决思路----------------------
录了个宏,修改了一下,直接使用通配符查找替换:
vba中如何对word指定范围中的字符,使用通配符(正则表达式)进行替换?
下面的代码希望:对tempRange2内“河”“湖”“江”等替换成*水*,都没有效果。
请指教。
谢谢!
'设定范围
Set tempRange2 = ActiveDocument.Range(Start:=startgb, End:=(startgb + tempend ))
'进行替换,方式1,不成功 这个在word中是直接可以用的,但在宏内却无效
tempRange2 = Replace(tempRange2, "[河湖江]", "*水*")
'进行替换,方式2,不成功
With Selection.Find
.Text = "[河湖江]*"
.Replacement.Text = "*水*"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
------解决思路----------------------
不好意思,word的vba不怎么用,你试试下面的行不行:
Sub Test()
Dim tempRange2 As Range
Set tempRange2 = ActiveDocument.Range(Start:=1, End:=50)
tempRange2.Text = RegStr(tempRange2.Text, "[河湖江]", "*水*")
End Sub
------解决思路----------------------
录了个宏,修改了一下,直接使用通配符查找替换:
Sub Test()
Dim tempRange2 As Range
Set tempRange2 = ActiveDocument.Range(Start:=1, End:=50)
ReplaceStrByTPF "江河湖", "水", tempRange2
End Sub
Sub ReplaceStrByTPF(ByVal findText As String, ByVal RepStr As String, Optional bRng As Range)
''使用通配符来替换
If bRng Is Nothing Then Set bRng = ThisDocument.Content
With bRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[" & findText & "]"
.Replacement.Text = RepStr
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True ''表明使用通配符
.Execute Replace:=wdReplaceAll
End With
End Sub