vba中如何对word指定范围中的字符,使用通配符(正则表达式)进行替换?

hztj2005 2014-12-31 11:36:23
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

...全文
2350 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hztj2005 2015-01-03
  • 打赏
  • 举报
回复
tempRange2.Text = RegStr(tempRange2.Text, "[河湖江]", "*水*") .MatchWildcards = True ''表明使用通配符 谢谢楼上!
一如既往哈 2015-01-01
  • 打赏
  • 举报
回复
录了个宏,修改了一下,直接使用通配符查找替换:
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


一如既往哈 2014-12-31
  • 打赏
  • 举报
回复
这个得一个一个的换吧?

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


一如既往哈 2014-12-31
  • 打赏
  • 举报
回复
不好意思,word的vba不怎么用,你试试下面的行不行:
Sub Test()
    Dim tempRange2 As Range
    Set tempRange2 = ActiveDocument.Range(Start:=1, End:=50)
    tempRange2.Text = RegStr(tempRange2.Text, "[河湖江]", "*水*")
End Sub


hztj2005 2014-12-31
  • 打赏
  • 举报
回复
谢谢楼上,可以用的,现在菜单-工具-引用-microsoft regular expression勾上,代码如下。 但还有一个问题,请教指定范围的tempRange2这个变量的文本正确替换后, 但word文件本身的文本未改变,如何使tempRange2应用到word文件本身?

 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

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧