VB.net Word 文档:添加新节后更新节号
VB.net Word document : update the section numbers after adding a new section
我正在复制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)
下面的代码在 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
我正在复制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)
下面的代码在 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