在每个循环开始时从文件开头开始循环遍历Word文档 [英] Loop through Word document, starting from beginning of file at start of each loop

查看:160
本文介绍了在每个循环开始时从文件开头开始循环遍历Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VBA中遍历Word文档.通过每遍,我想搜索一个不同的单词,因此,每遍都需要从文件的开头开始.我正在使用的当前方法是这种方法,但是它似乎在搜索第一个单词,当它到达文件的末尾时,移到第二个单词,并且仅在文件的末尾搜索,依此类推.剩下的话.

I'm looping through a Word Document in VBA. Through each pass I'd like to search for a different word, so each pass needs to start at the beginning of the file. The current method I'm using is this, but it appears to search for the first word, and when it gets to the end of the file, moves to the second word, and searches only at the end of the file and so on with the rest of the words.

'my array of words is called myKeywords
For x = LBound(myKeywords) To UBound(myKeywords)
    PageNumbers(x) = ""
    'start from start of file here somehow.
    With oWord.Selection.Find
        .ClearFormatting()
        .Text = myKeywords(x)
        .Wrap = False
        .Forward = True
        Do While .Execute = True
            If PageNumbers(x) = "" Then
                PageNumbers(x) = oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
            Else
                PageNumbers(x) = PageNumbers(x) & ", " & oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
            End If
        Loop
    End With
Next

任何见识都会受到赞赏.

Any insight is appreciated.

编辑1

我已阅读,但我看不到有什么可以帮助我的. Wrap看起来会有所帮助,但是如果我设置wrap = True,则会收到错误值超出范围",并且我看不到.wrap如何将任何内容设置为超出范围.

I have read through this, and I don't see anything that could help me. Wrap looks like it would help, but if I set wrap = True, I get the error "Value out of range", and I don't see how .wrap could set anything out of range.

编辑2

我尝试设置范围并在循环结束时将其重置,但到目前为止没有成功.

I tried setting a range and resetting it at the end of the loop, but no success so far.

'my array of words is called myKeywords
Range = oWord.Selection
For x = LBound(myKeywords) To UBound(myKeywords)
    PageNumbers(x) = ""
    'start from start of file here somehow.
    With Range.Find
        .ClearFormatting()
        .Text = myKeywords(x)
        .Wrap = False
        .Forward = True
        Do While .Execute = True
            If PageNumbers(x) = "" Then
                PageNumbers(x) = oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
            Else
                PageNumbers(x) = PageNumbers(x) & ", " & oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
            End If
        Loop
        'I've also tried several other variations of Range = other things to get this to work.
        Range = Range.Select

    End With
Next

编辑3

'my array of words is called myKeywords
Range = oWord.ActiveDocument.Range(Start:=oWord.Selection.Start, [End]:=oWord.Selection.End)
For x = LBound(myKeywords) To UBound(myKeywords)

    'start from start of file here somehow.
    With oWord.Selection.Find
        PageNumbers(x) = ""
        .ClearFormatting()
        .Text = myKeywords(x)
        .Wrap = False
        .Forward = True
        Do While .Execute = True
            If PageNumbers(x) = "" Then
                PageNumbers(x) = oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
            Else
                PageNumbers(x) = PageNumbers(x) & ", " & oWord.Selection.Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
            End If
        Loop
        Range.Select()

    End With
Next

推荐答案

您将Find应用于当前选择.但是选择通过调用Find对象上的Execute来移动.结果是,在循环的第一次运行之后,选择是在第一个动词的最后一次出现时进行的.这将是下一个Find.Execute的起点.一种可能的解决方案是在循环开始时将当前选择存储在Range变量(range=oWord.Selection)中,然后在循环结束时将选择设置回该存储范围(range.Select). (可以肯定的是,对于您的一般性问题有更好的解决方案,但这可以解决您的具体问题.)

You apply the Find to the current selection. But the selection moves by calling the Execute on the Find object. The consequence is that after the first run of the loop the selection is on the last occurence of the first verb. This will be the starting point for the next Find.Execute. One possible solution is to store the current selection at the start of the loop in a Range variable (range=oWord.Selection) and set the selection back to this stored range at the end of the loop (range.Select). (For sure there are more elegant solutions for your general problem but it could be a solution to your concrete problem.)

这篇关于在每个循环开始时从文件开头开始循环遍历Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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