|
楼主 |
发表于 2019-5-28 14:29
|
显示全部楼层
本帖最后由 tangqingfu 于 2019-5-28 17:03 编辑
以下是杜老师帮写的代码(将关键字所在的段落提取到新文档中),可惜无法做到保持原有格式(颜色),请教如何修改代码做到提取后保留原有格式(如字体,颜色等)?
- Sub main()
- Dim doc As Document, re As Object, mh As Object
- Dim mystr$, keystr$, restr$, results$, i&, n&
- If Selection.Type = wdSelectionIP Then
- MsgBox "没选择关键字!": Exit Sub
- Else
- keystr = Selection.Text '选择文档中某一处“关键字”
- End If
- For i = 1 To Len(keystr)
- restr = restr & "\u" & Right("0000" & Hex$(AscW(Mid(keystr, i, 1))), 4)
- Next
- Set doc = ActiveDocument
- mystr = doc.Content.Text
- Set re = CreateObject("vbscript.regexp")
- re.Global = True: re.Pattern = restr
- Dim d As Object
- Set d = CreateObject("Scripting.Dictionary")
- For Each mh In re.Execute(mystr)
- i = mh.FirstIndex: n = mh.Length
- With doc.Range(i, i + n)
- .Expand 4
- d(.Text) = vbNullString
- End With
- Next
- For Each k In d.keys
- results = results & k
- Next
- With Documents.Add
- .Content.InsertAfter results
- End With
- End Sub
复制代码
|
|