Word 2016 VBA循环直到文档结尾 [英] Word 2016 VBA loop until end of document

查看:899
本文介绍了Word 2016 VBA循环直到文档结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线上查看了许多不同的答案,但无法找到适合我的代码的解决方案.这是我第一次用Word编写VBA(对Excel有一定的经验).

I've looked at many different answers online but have not been able to find a solution that fits my code. This is my first time writing VBA in Word (have some moderate experience in Excel).

我认为此帖子可能是我需要但它不会对我停止文档末尾的循环.

I thought this post might be what I need but it doesn't stop the loop at the end of the document for me.

我正在尝试在新节开始之前插入一个连续的分节符,我将其指定为采用标题1样式设置的文本.见解!

I'm trying to insert a continuous section break before the start of a new section, which I designate as text that is formatted with style Heading 1. I'm totally open to doing this another way and would be grateful for your insights!

Sub InsertSectionBreak()
    ' Go to start of document
    Selection.HomeKey Unit:=wdStory

    ' Find next section based on header formatting, insert continuous section break just before
    '
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 1")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    Do While Selection.Find.Execute = True
        Selection.Find.Execute
        Selection.MoveLeft Unit:=wdCharacter, Count:=1
        Selection.InsertBreak Type:=wdSectionBreakContinuous
    Loop
End Sub

推荐答案

问题中的代码还不错,但是有一个主要问题:Selection朝着文档的前面移动,以便插入分节符.这意味着下次Find再次运行时,它会找到相同的标题1,从而在同一位置重复插入分节符.

The code in the question is not bad, but has a major problem: The Selection is being moved towards the front of the document in order to insert the Section Break. This means that next time Find runs it again finds the same Heading 1 and thus repeatedly inserts Section Breaks in the same place.

另一个问题是代码正在作为Do While标准的一部分执行Find(这就是为什么它没有在文档中找到标题1的第一个实例)的原因.

The other problem is that the code is executing Find as part of the Do While criterium (which is why it's not finding the first instance of Heading 1 in the document).

下面的代码示例适用于Range对象而不是Selection.您可以将范围想象为一个看不见的选择,但有一个非常重要的区别:可以有多个范围;只能有一个选择.

The following code sample works with Range objects instead of the Selection. You can think of a Range like an invisible selection with a very important difference: there can be multiple Ranges; there can be only one selection.

建议的代码使用两个范围:一个用于查找,另一个用于插入分节符.查找范围设置为整个文档.查找是否成功存储在布尔变量(bFound)中.

The suggested code uses two ranges: one for the Find and the other for inserting the Section Break. The Find range is set to the entire document. Whether the Find is successful is stored in a boolean variable (bFound).

如果查找成功,则找到的范围将复制到分节符的范围. Duplicate对原始范围进行独立的复制",以便可以彼此独立地进行操作.然后将分节符的范围缩小到起点(可以像按向左箭头那样思考),然后插入分节符.

If Find is successful the found range is duplicated to the range for the Section break. Duplicate makes an independent "copy" of the original range so that they can be manipulated independently of one another. The range for the section break is then collapsed to its starting point (think of it like pressing left-arrow), then the section break is inserted.

但是,将查找"范围折叠到其终点,以便将其移动到使用标题1设置格式的文本之外,从而可以定位下一个标题1.然后再次执行查找,并重复循环,直到找不到标题1的更多实例为止.

The Find range, however, is collapsed to its end point in order to move it beyond the text formatted with Heading 1 so that the next Heading 1 can be targeted. Find is then executed again and the loop repeats until no more instances of Heading 1 are found.

Sub InsertSectionBreak()
    Dim rngFind As Word.Range, rngSection As Word.Range
    Dim bFound As Boolean

    Set rngFind = ActiveDocument.content

    ' Find next section based on header formatting, insert continuous section break just before
    '
    rngFind.Find.ClearFormatting
    rngFind.Find.style = ActiveDocument.styles("Heading 1")
    With rngFind.Find
        .text = ""
        .Replacement.text = ""
        .Forward = True
        .wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        bFound = .Execute
    End With

    Do While bFound
        Set rngSection = rngFind.Duplicate
        rngSection.Collapse wdCollapseStart
        rngSection.InsertBreak Type:=wdSectionBreakContinuous
        rngFind.Collapse wdCollapseEnd
        bFound = rngFind.Find.Execute
    Loop
End Sub

这篇关于Word 2016 VBA循环直到文档结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆