首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VB.net Word文档:添加新节后更新节号

VB.net Word文档:添加新节后更新节号
EN

Stack Overflow用户
提问于 2020-04-20 03:33:58
回答 1查看 168关注 0票数 0

我正在复制word文档的第8节,并将其粘贴在第8节的末尾,然后在刚刚粘贴的部分中替换一些单词。在此之后,我必须再次复制第8节,并将其粘贴到先前粘贴的部分的末尾(即第9节)。

问题是,我有多达10节,当我第一次复制和粘贴第8节时,它会正确地粘贴,但是当我第二次粘贴时,它被粘贴在第10节,而不是在最近粘贴的部分的末尾。

下面是我的代码,我使用"goto“将光标放置在节的末尾,并增加节号。

代码语言:javascript
运行
复制
 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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-20 08:19:51

下面的代码是用VBA编写的,但演示了实现目标的一种方法。请注意,此代码只是插入新节并将文本从特定节复制到新节。它不对节进行任何编辑。

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61314732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档