vba字:在标题之间选择文本 [英] word vba: select text between headings
问题描述
我有一个Word文档,在其中我想选择标题的全文(从枚举2.3.1开始),直到(不包括)标题2.3.2或[文件结尾].如果中间有较小的"小节或图片或表格,则也应选择它们.
I have a word document in which I would like to select the full text of the heading starting with enumeration 2.3.1 until (not included) the heading 2.3.2 or [End of File]. If there are 'smaller' subsections or pictures or tables in between, they should also be selected.
PS:示例:
...
2.2 Blah
Blah
2.3 Blubb
Blubb
[Start Selection]
2.3.1 Important1
Important2
[Picture: Important3]
[Table: Important4]
2.3.1.1 Important 5
Important 6
[Stop Selection]
2.3.2 Blieh
...
2.2 Blah
Blah
2.3 Blubb
Blubb
[Start Selection]
2.3.1 Important1
Important2
[Picture: Important3]
[Table: Important4]
2.3.1.1 Important 5
Important 6
[Stop Selection]
2.3.2 Blieh
我已经尝试过浏览每个段落,但这很慢.我需要此功能以在以后复制选择内容(我已经知道该怎么做了;-)).
I have experimented with navigating through every paragraph, but this is quite slow. I need this feature to copy the selection afterwards (I already know how to do that ;-)).
非常感谢您的帮助!
Jan
推荐答案
这似乎效果很好.
调整格式设置,使其仅在该给定的格式类型中找到"2.3.1"等.
This seems to work well.
Adjust the format setting so that it finds '2.3.1' etc. only in that given format type.
Sub Macro1()
Selection.WholeStory
Selection.Collapse wdCollapseStart
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Caption 1")
With Selection.Find
.Text = "2.3.1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
End With
Selection.Find.Execute
Selection.Collapse wdCollapseStart
Dim r1 As Range
Set r1 = Selection.Range
' keep format settings, only change text
Selection.Find.Text = "2.3.2"
If Selection.Find.Execute Then
Selection.Collapse wdCollapseStart
Else
Selection.WholeStory
Selection.Collapse wdCollapseEnd
End If
Dim r2 As Range
Set r2 = ActiveDocument.Range(r1.Start, Selection.Start)
r2.Select
End Sub
这篇关于vba字:在标题之间选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!