2,462
社区成员
发帖
与我相关
我的任务
分享
'设定范围
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
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
Sub Test1()
Dim i As Integer, w1 As String, ww
w1 = "ww,123,rt"
ww = Split(w1, ",")
Dim tempRange2 As Range, startgb As Long, tempend As Long
startgb = 1
tempend = 50
Set tempRange2 = ThisDocument.Range(Start:=startgb, End:=(startgb + tempend))
For i = 0 To UBound(ww)
ReplaceStr ww(i), "水", tempRange2
Next
End Sub
Function ReplaceStr(ByVal findText As String, ByVal RepStr As String, Optional bRng As Range) As Boolean
If bRng Is Nothing Then Set bRng = ThisDocument.Content
With bRng.Find
.ClearFormatting
.Text = findText
.Replacement.ClearFormatting
.Replacement.Text = RepStr
.Execute Replace:=wdReplaceAll, Forward:=True
ReplaceStr = .Found
End With
End Function
Sub Test()
Dim tempRange2 As Range
Set tempRange2 = ActiveDocument.Range(Start:=1, End:=50)
tempRange2.Text = RegStr(tempRange2.Text, "[河湖江]", "*水*")
End Sub
Set tempRange2 = ActiveDocument.Range(Start:=startgb, End:=(startgb + tempend + 2))
tempRange2 = RegStr(nowstr, ""[河湖江]", ", "*水*")
'正则表达式
Function RegStr(ByRef str As String, patt As String, change As String) As String
Dim arrStr As String
With CreateObject("VBSCRIPT.REGEXP") ' 生成一个正则表达式对象实例
.Global = True ' 设置全局可用,即替换所有符合匹配模式的字符串
.Pattern = patt ' 匹配模式为非大写字母
arrStr = .Replace(str, change) ' 将arr(i)字符串中符合匹配模式的部分替换为空字符
End With
RegStr = arrStr
End Function