|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
提取含有某一关键词的所有段落到新文档展示
- Sub 提取含有某一关键词的所有段落()
- Dim adoc As Document
- Dim newDoc As Document
- Dim pa As Paragraph
- Dim s As String
- Dim i As Long
- Dim t0 As Single
-
- t0 = Timer
- s = InputBox("请输入您的关键词(支持Word通配符):")
- If s = "" Then
- MsgBox "关键词为空,将退出程序!"
- Exit Sub
- End If
- Set adoc = ActiveDocument
- Set newDoc = Documents.Add
- i = 0
-
- For Each pa In adoc.Paragraphs
- pa.Range.Find.ClearFormatting
- If pa.Range.Find.Execute(findtext:=s, Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) Then
- i = i + 1
- pa.Range.Copy
- newDoc.Paragraphs.Last.Range.Select
- With Selection
- .Collapse 0
- .PasteAndFormat (wdFormatOriginalFormatting)
- End With
- 'newDoc.Paragraphs.Last.Range.InsertAfter Chr(13)
- End If
- Next
-
- newDoc.Paragraphs.Last.Range.Delete
- MsgBox Format(i, "共0段,") & Format(Timer - t0, "提取完成!用时0.000秒!")
- newDoc.Windows(1).Activate
- Set adoc = Nothing
- Set newDoc = Nothing
- End Sub
复制代码
|
|