|
参照下面的代码:
Sub ChooseContent()
Dim rng As Range
Dim sArr() As Variant
Dim eArr() As Variant
Dim ListC, TableC, FieldC, InshapeC, Cnt As Integer
With ActiveDocument
ListC = .ListParagraphs.Count
TableC = .Tables.Count
FieldC = .Fields.Count
InshapeC = .InlineShapes.Count
Cnt = ListC + TableC + FieldC + InshapeC
ReDim sArr(Cnt - 1)
ReDim eArr(Cnt - 1)
If ListC <> 0 Then '获取带列表的段落位置
For i = 0 To ListC - 1
sArr(i) = .ListParagraphs.Item(i + 1).Range.Start
eArr(i) = .ListParagraphs.Item(i + 1).Range.End
Next
End If
If TableC <> 0 Then '获取表格的位置
For i = ListC To ListC + TableC - 1
sArr(i) = .Tables.Item(i + 1 - ListC).Range.Start - 1
eArr(i) = .Tables.Item(i + 1 - ListC).Range.End
Next
End If
If FieldC <> 0 Then '获取题注的位置
For i = ListC + TableC To ListC + TableC + FieldC - 1
sArr(i) = .Fields.Item(i + 1 - ListC - TableC).Result.Paragraphs(1).Range.Start
eArr(i) = .Fields.Item(i + 1 - ListC - TableC).Result.Paragraphs(1).Range.End
Next
End If
If InshapeC Then '获取内嵌图片位置
For i = ListC + TableC + FieldC To ListC + TableC + FieldC + InshapeC - 1
sArr(i) = .InlineShapes.Item(i + 1 - ListC - TableC - FieldC).Range.Start
eArr(i) = .InlineShapes.Item(i + 1 - ListC - TableC - FieldC).Range.End + 1
Next
End If
Call BubbleSort(sArr, Cnt - 1) '排序
Call BubbleSort(eArr, Cnt - 1)
If sArr(0) <> 0 Then '前文字
Set rng = .Range(Start:=0, End:=sArr(0))
rng.Editors.Add wdEditorEveryone
End If
For i = 0 To Cnt - 2 '间文字
If eArr(i) < sArr(i + 1) Then
Set rng = .Range(Start:=eArr(i), End:=sArr(i + 1))
End If
rng.Editors.Add wdEditorEveryone
Next
If eArr(Cnt - 1) <> .Range.End Then '后文字
Set rng = .Range(Start:=eArr(Cnt - 1), End:=.Range.End)
End If
.SelectAllEditableRanges wdEditorEveryone
.DeleteAllEditableRanges wdEditorEveryone
End With
End Sub
Function BubbleSort(ByRef byArr() As Variant, bySize As Integer)
Dim TEMP As Variant
For i = 0 To bySize - 2
For j = i + 1 To bySize - 1
If byArr(i) > byArr(j) Then
TEMP = byArr(j)
byArr(j) = byArr(i)
byArr(i) = TEMP
End If
Next j
Next i
End Function |
|