一起分享一下正则表达式的简便吧! '* +++++++++++++++++++++++++++++ '* Created By SHOUROU@ExcelHome 2008-3-7 6:54:15 '仅测试于System: Windows NT Word: 11.0 Language: 2052 '№ 0352^The Code CopyIn [ThisDocument-ThisDocument]^' '* ----------------------------- Option Explicit '显示变量声明 Option Compare Text '不区分大小写 Dim RegExp_ReplaceCount As Long Sub myReplace() '运行本过程前需在VBE/工具/引用中勾选"Microsoft Forms 2.0 Object Library" Dim myDataObject As New DataObject, myDoc As Document Dim mySourceString As String, sinStart As Single, sinEnd As Single sinStart = Timer With ActiveDocument .Content.Cut '剪切 .UndoClear '清空撤消(由于文档太大,应注重这方面的考虑,节约内存,加速运行) myDataObject.GetFromClipboard '将剪贴板数据传递到DataObject mySourceString = myDataObject.GetText(1) '取得无格式文本 Set myDataObject = Nothing '释放对象变量 .Content.Text = VBS_Replace(mySourceString, "(MatchPercent=""100"".*?Font.*?>)", "$1▼") sinEnd = Timer MsgBox "Word历时" & sinEnd - sinStart & "秒,执行了" & _ RegExp_ReplaceCount & "次查找和替换!", vbInformation, "Microsoft VBScript" End With End Sub '---------------------- Function VBS_Replace(strSource As String, strFindText As String, strRepText As String) As String '请在VBE中引用 Microsoft VBScript Regular Expressions 5.5 '本程序适用于取得匹配查找内容的数量或者无格式复杂替换 Dim myRegEx As New VBScript_RegExp_55.RegExp '定义一个新的正则表达式对象 Dim Matches As Object myRegEx.IgnoreCase = True ' 不区分大小写 myRegEx.Global = True ' 搜索全部 myRegEx.Pattern = strFindText '设置模式表达式 Set Matches = myRegEx.Execute(strSource) RegExp_ReplaceCount = Matches.Count '返回匹配查找内容的数量 VBS_Replace = myRegEx.Replace(strSource, strRepText) End Function '----------------------
Nt30ysFF.rar
(15.39 KB, 下载次数: 103)
[此贴子已经被作者于2008-3-7 6:56:32编辑过] |