正则表达式替换在字(词)宏 - Regex replacing in Word macros

- 此内容更新于:2015-12-20
主题:

是否可以使用替代词VBA模式与对象?我可以搜索文档模式和一群括号换成类似的吗?(我试过了,但是它使用了文字字符串)。

原文:

Is it possible to use substitution patterns with RegExp objects in Word VBA? Could I search a document for a pattern with a bunch of parentheses and replace it with something like \4 \2? (I tried, but it used the literal string.)

解决方案:
看到这个答案信息和VBA正则表达式。虽然这特定的回答是写给ExcelVBA具体地说,它仍将适用于词。在示例代码中,他集而不是你能做的,和替换现有的答案是一个强大的工具。有时是有用的看ExcelVBA代码为所有其他微软Excel应用程序是为它编写的代码的工具。
原文:

See this answer for information on RegEx with VBA. While this specific answer was written for Excel VBA specifically, it will still apply to word.

In the sample code, he sets MyRange = ActiveSheet.Range("A1") instead you could do, Set MyRange = ActiveDocument.Range(Start:=0, End:=Selection.End) and replace MyRange.Value with MyRange.Text

Existing answers are a powerful tool. Sometimes it's helpful to look at Excel VBA code for all the other Microsoft Applications as Excel is the tool which has code written for it the most.

解决方案:
词,本身不支持正则表达式。它有自己的通配符功能。这类似于正则表达式而不是相同的。你可以看到可能性替换对话框中,点击“更多”(Ctrl+H),激活“通配符”复选框,然后看着“特别”的列表中。如果你在Internet上搜索一组术语像字找到替代通配符——你会出现大量的例子和讨论。JDB_Dragon通常建议的方法通常是不令人满意的词因为操纵字符串然后写回失去所有格式的文档。
原文:

Word, itself, does not support RegEx. It has its own Wildcard functionality. This is similar to RegEx but not identical. You can see the possibilities by clicking "More" in the Replace dialog box (Ctrl+H), activating the "Wildcard" checkbox, then looking at the list in "Special". If you search the Internet for a set of terms like - Word Find Replace Wildcard - you'll turn up lots of examples and discussions.

The approach suggested by JDB_Dragon usually is often not satisfactory in Word because manipulating just the string then writing it back to the document loses all formatting.