[英]MS Word 2016 Macro Regex Replace
I am trying to code a macro using Regex
and my pattern is \\(\\bTopic \\d+\\b\\)
and macro should remove (Topic 1) and (Topic 2) from all questions in the whole document, but my macro code is not working. 我正在尝试使用
Regex
编写宏,我的模式是\\(\\bTopic \\d+\\b\\)
,宏应该从整个文档中的所有问题中删除(主题1)和(主题2) ,但我的宏代码不是工作。 Thanks for your help. 谢谢你的帮助。
Input: 输入:
QUESTION NO: 1 (Topic 1) 问题编号:1(主题1)
QUESTION NO: 15 (Topic 2) 问题编号:15(主题2)
Result should be 结果应该是
QUESTION NO: 1 问题编号:1
QUESTION NO: 15 问题编号:15
Macro Code 宏代码
Sub RemoveQuestionTopic()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\(\bTopic \d+\b\)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
The regexp syntax differs from normal regexp. regexp语法与普通正则表达式不同。 If you replace your expression with this:
如果用这个替换表达式:
.Text = "\(Topic ([0-9]@>)\)"
you will get the expected result. 你会得到预期的结果。 No need to use VBScript.Regexp in this case.
在这种情况下,无需使用VBScript.Regexp。
The Find functionality provide only a very limited support to the regular expressions. 查找功能仅对正则表达式提供非常有限的支持。 You need to use
VBScript.Regexp
as shown below. 您需要使用
VBScript.Regexp
,如下所示。
Try this Code: 试试这个代码:
Sub RemoveQuestionTopic()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
strReplacementText = "++__++"
ActiveDocument.Select
Set objReg = CreateObject("VBScript.Regexp")
objReg.Pattern = "\s*\(Topic\s*\d+\)"
objReg.Global = True
Selection.Text = objReg.Replace(Selection.Text, strReplacementText)
With Selection.Find
.MatchWildcards = True
.Text = strReplacementText
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.