VBA 当我恢复修订时每个循环都被锁定

VBA for each loop locked when I revert revisions

我想从文档中同时获取修改后的文本和原始文本。我这样做:

Set wrdDoc = wrdApp.Documents.Open(fileName)

For each sent in wrdDoc.Sentences

    if sent.Revisions.Count >=0 then
        after=sent.text
        sent.Revisions.RejectAll 
        before=sent.text
        SaveRev(before,after)
    End if

next

这样就好了,除了像

这样的畸形句子

This is one sentence.This is another.

将以一种奇怪的方式进行解析。首先,会有这样一个:“这是一个句子。”,然后是这两个“这是一个 sentence.This 是另一个。"

那里有修改时会发生什么?第一次迭代将恢复对第一句话的修订,然后第二次迭代将不会 'see' 修订的部分。

底线是,第一次迭代将获得第一句的两个版本,第二次迭代将仅获得第一句的原始版本(同时从第二句获得两个版本)。

让我澄清一下:

假设我有原件

We started with this sentence.And this sentence.

修改为

We ended with this sentence.And this other sentence.

第一次迭代将导致

Before: We started with this sentence.

After: We ended with this sentence.

但是第二次迭代会有

Before: We ended with this sentence.And this sentence.

After: We ended with this sentence.And this other sentence.

好吧,我所做的是改变逻辑,撤消修订版本:

Set wrdDoc = wrdApp.Documents.Open(fileName)

For each sent in wrdDoc.Sentences

    if sent.Revisions.Count >=0 then
        wrdDoc.Undo
        after=sent.text
        sent.Revisions.RejectAll 
        before=sent.text
        SaveRev(before,after)
    End if

next

我喜欢这个,因为我最终得到了一个未更改的文档(除了最后一句话)。

事情是,这样做会使宏在一个特定的句子处陷入无限循环。

我不知道 for each 的机制,我不知道是什么导致它挂起。显然改变集合会打乱循环,但我不明白为什么。

我可以循环 i=0 到 wrdDoc.Sentences.Count,但我认为这会让我跳过句子,原因与我现在重复一个相同,我不能冒险(即使我测试 OK ,我必须确保它永远不会发生)。

所以问题是(是):

  1. 谁能帮我弄清楚为什么它会锁定一个句子,
  2. 有更好的方法吗?
  3. 如何在不跳句的情况下解决

非常感谢!

PS:我可以提供示例文档,如果需要请告诉我(也许我做错了什么已经有人清楚了,我不得不制作示例,因为我无法分享我正在处理的文档)。

--编辑--

好的,这就是它挂的地方,只在第 32 个文件上。

它不会挂在一个句子上,它实际上在文档的开头做了一些,然后回到开头。

我之前也遇到过同样的错误,但是单句循环,没有回到开头。我认为这是同一个问题。我将尝试在此处复制原始版本和修订版本。

原版

MAIN TITLE

Measurement of some variable

1   REQUIRED TOOLS

1.1 Special tools

NOTe:

Some note about the procedure (unaltered by revision)

Equipment name (carrier returned line)
(english) assemply with Equipment PN
Kit

Equipment name (carrier returned line)
(english) assemply with (Another) Equipment PN
Kit

Document continues...

在重新开始循环之前有 2 个设备条目。

修订包括插入文件编号、一些首字母大写以及更改设备 PN 和“套件”之间的顺序。

修订版

ducument number
MAIN TITLE

Measurement of Some Variable

1   REQUIRED TOOLS

1.1 Special Tools

NOTe:

Some note about the procedure (unaltered by revision)

Equipment name (carrier returned line)
(english) assemply with kit
Equipment PN


Equipment name (carrier returned line)
(english) assemply with kit
(Another) Equipment PN


Document continues...

记录的 original/revison 对是:

原......................................修订

{空}.................................文档编号

一些变量的测量.................一些变量的测量

专用工具......................专用工具

(english) assembly with................................(english) assembly with kit

(english) assembly with................................(english) assembly with kit

然后它再次开始,记录相同的条目,直到我休息。 我没有看到我说的句子重叠,但是修改时有一个换行符插入。

谢谢!

枚举 objects 不应在枚举期间更改,否则可能会发生不好的事情(这取决于 collection 的类型)。

我的猜测是 revision/undo 过程与不稳定的句子相结合,导致可枚举的句子发生变化。

您应该先准备好自己的 collection,看看这是否会有所不同。只需尝试 Set sents = New Collection: For Each sent in wrdDoc.Sentences: sents.Add sent: Next,然后将 sents 用于您的主 For Each 循环。