我使用书签来标识标题,然后简单地返回它们之间的文本。但我不知道你不怀好意的But What I want is the List (not paragraph of the list)
截图
代码
Option Explicit
Sub Sample()
Dim MyRange As Range
Selection.HomeKey Unit:=wdStory
On Error Resume Next
ActiveDocument.Bookmarks("MYStartBookMark").Delete
ActiveDocument.Bookmarks("MYEndBookMark").Delete
On Error GoTo 0
'~~> Find Heading 1
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Heading 1")
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Execute
End With
'~~> Move one space to the right
Selection.MoveRight Unit:=wdCharacter, Count:=1
'~~> Insert the start Book mark
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="MYStartBookMark"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
'~~> Find Heading 2
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Heading 2")
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Execute
End With
'~~> Move one space to the left
Selection.MoveLeft Unit:=wdCharacter, Count:=1
'~~> Insert the end Book mark
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="MYEndBookMark"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
'~~> Identify the range between the Start BookMark and End BookMark
Set MyRange = ActiveDocument.Range
MyRange.Start = MyRange.Bookmarks("MYStartBookMark").Range.End
MyRange.End = MyRange.Bookmarks("MYEndBookMark").Range.Start
'~~> This gives you that text
Debug.Print MyRange.Text
'~~> Delete the BookMarks
On Error Resume Next
ActiveDocument.Bookmarks("MYStartBookMark").Delete
ActiveDocument.Bookmarks("MYEndBookMark").Delete
On Error GoTo 0
End Sub
结果什么
其他测试
有人可能会说,如果我们不知道是什么的一个标题是什么?这是一个公平点,因为我们可以有两个场景。让我一起
- 覆盖它们标题1之后,我们标题3
- 文档中的最后一个标题为标题1,之后没有标题。
修改后的代码
Option Explicit
Sub Sample()
Dim MyRange As Range
Dim MyArray
Dim strOriginal As String, strTemp As String
Dim numDiff As Long, i As Long, NextHd As Long
Dim NoNextHeading As Boolean
Selection.HomeKey Unit:=wdStory
On Error Resume Next
ActiveDocument.Bookmarks("MYStartBookMark").Delete
ActiveDocument.Bookmarks("MYEndBookMark").Delete
On Error GoTo 0
'~~> Get all the headings in the array
NoNextHeading = True
For i = LBound(MyArray) To UBound(MyArray)
strOriginal = RTrim$(MyArray(i))
strTemp = LTrim$(strOriginal)
numDiff = Len(strOriginal) - Len(strTemp)
numDiff = (numDiff/2) + 1
'~~> If heading one is found and it is not the last heading
'~~> in the array then find what is the next heading
If numDiff = 1 And i <> UBound(MyArray) Then
strOriginal = RTrim$(MyArray(i + 1))
strTemp = LTrim$(strOriginal)
numDiff = Len(strOriginal) - Len(strTemp)
numDiff = (numDiff/2) + 1
NextHd = numDiff
NoNextHeading = False
Exit For
End If
Next i
'~~> Find Heading 1
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Heading 1")
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Execute
End With
'~~> Move one space to the right
Selection.MoveRight Unit:=wdCharacter, Count:=1
'~~> Insert the start Book mark
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="MYStartBookMark"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
If NoNextHeading = False Then
'~~> Find Heading NextHd
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Heading " & NextHd)
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Execute
End With
'~~> Move one space to the left
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Else
'~~> Move to the end of the document
ActiveDocument.Characters.Last.Select
Selection.Collapse
End If
'~~> Insert the end Book mark
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="MYEndBookMark"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
'~~> Identify the range between the Start Book Mark and End BookMark
Set MyRange = ActiveDocument.Range
MyRange.Start = MyRange.Bookmarks("MYStartBookMark").Range.End
MyRange.End = MyRange.Bookmarks("MYEndBookMark").Range.Start
'~~> This give you that text
Debug.Print MyRange.Text
'~~> Delete the BookMarks
On Error Resume Next
ActiveDocument.Bookmarks("MYStartBookMark").Delete
ActiveDocument.Bookmarks("MYEndBookMark").Delete
On Error GoTo 0
End Sub
'但是我想是List(不在名单的段落)'我不知道我知道......我可以检索和之间的文本但我不确定你的意思。 –
2013-04-21 07:40:11