可以使用InsertParagraphBefore
或InsertParagraphAfter
将段落随时插入Word文档中。但是,一旦文档中有表,则如果插入点位于表的前面或后面,则将以不同的方式执行该指令,如果插入点位于文档的末尾,则将以不同的方式执行该指令。我花了一个漫长的星期天下午研究此主题,并创建了一个函数,该函数在期望的位置插入一个段落。我邀请提出改进建议,或者甚至使它变得无关紧要,正如我认为的那样。
答案 0 :(得分:0)
Function InsertBlankPara(Rng As Range, _
Optional ByVal Before As Boolean) As Range
' SSY 001 01 Nov 2020
' ======================================================
' Rng may be a single paragraph or part thereof
' or a table or part thereof
' Before Insert the paragraph before Rng if True
' [False by default, appending the paragraph]
' ======================================================
' Return value the inserted paragraph
' Return object Rng will expand the original Rng to include the
' entire table or paragraph it comprised originally
' plus the paragraph that was added
' ======================================================
Dim Pstart As Long
Dim Pend As Long
Dim IsTbl As Boolean
With Rng
IsTbl = .Information(wdWithInTable)
.Expand IIf(IsTbl, wdTable, wdParagraph)
Pstart = .Start
Pend = .End + 1
.Collapse IIf(Before, wdCollapseStart, wdCollapseEnd)
If IsTbl Then .Move wdCharacter, IIf(Before, -1, _
IIf(.Document.Range.End <= Pend, 2, 0))
If IsTbl Or Before Then
.InsertParagraphBefore
Else
.Move wdCharacter, IIf(.Document.Range.End < Pend, 0, -1)
.InsertParagraphAfter
End If
Set Rng = .Document.Range(Pstart, Pend)
If Before Then
Pend = Pstart + 1
Else
Pstart = Pend - 1
End If
Set InsertBlankPara = .Document.Range(Pstart, Pend)
End With
End Function