我正在复制单词文档的第8节,并将其粘贴在第8节的末尾。之后,我在刚粘贴的部分中替换了一些单词。之后,我必须再次复制第8节,并将其粘贴到先前粘贴的节的末尾(即第9节)。
问题是,最初我最多可以有10个节,当我第一次复制并粘贴第8节时,它会正确粘贴,但是当我第二次粘贴时,它会粘贴到第10节的位置,而不是最近粘贴的末尾部分。
下面是我的代码,我使用“ goto
”将光标放在节的末尾并增加节号。
Function copyPasteSectionInWord(copysectionnumber As String, PastelastOfThisSectionnumber As String)
Dim sectionInFocus As Microsoft.Office.Interop.Word.Section
Dim secRange As Microsoft.Office.Interop.Word.Range
sectionInFocus = wordDoc.Sections.Item(copysectionnumber)
secRange = sectionInFocus.Range
secRange.Copy()
wordApp.Selection.GoTo(What:=WdGoToItem.wdGoToSection, Which:=WdGoToDirection.wdGoToNext, Count:=PastelastOfThisSectionnumber)
'secRange2.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd)
'secRange.Paste()
wordApp.Selection.Paste()
'wordDoc.Fields.Update()
'secRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd)
releaseObject(secRange)
下面的代码在VBA中,但是演示了实现目标的一种方法。请注意,此代码仅插入新部分并将文本从特定部分复制到新部分。它不对节进行任何编辑。
Sub InsertSectionsandPopulateWithText()
Dim myRange As Word.Range
Set myRange = ActiveDocument.Sections.Item(8).Range
myRange.Collapse direction:=wdCollapseEnd
myRange.InsertBreak Type:=WdBreakType.wdSectionBreakNextPage
myRange.InsertBreak Type:=WdBreakType.wdSectionBreakNextPage
With ActiveDocument.Sections
.Item(9).Range.FormattedText = .Item(8).Range.FormattedText
.Item(10).Range.FormattedText = .Item(8).Range.FormattedText
End With
End Sub