我正在复制word文档的第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)发布于 2020-04-20 08:19:51
下面的代码是用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 Subhttps://stackoverflow.com/questions/61314732
复制相似问题