在两个文档中进行更改时 StartCustomRecord 不起作用

StartCustomRecord does not work when making changes in two documents

我编写了一段代码,可以在一个撤消自定义记录中对两个 Word 文档进行更改

例如:

Sub TestUndoRecord()

    Dim objUndo As UndoRecord
    Set objUndo = Application.UndoRecord
    objUndo.StartCustomRecord "TEST"

    Application.Documents(2).Activate
    Application.Documents(2).Content.Paragraphs.first.Range.Text = "1"

    Application.Documents(1).Activate
    Application.Documents(1).Content.Paragraphs.first.Range.Text = "1"

    Application.Documents(2).Activate
    Application.Documents(2).Content.Paragraphs.first.Range.Text = "2"

    Application.Documents(1).Activate
    Application.Documents(1).Content.Paragraphs.first.Range.Text = "2"

    objUndo.EndCustomRecord

End Sub

在命令结束时,取消列表中出现了三行,而不是带有单词 TEST 的一行。 如何解决这个问题?

我的示例在 VBA 中,但如果能以 C# 或 vb.net 得到答案,我将不胜感激。

撤消列表是特定于文档的:您不能拥有一个涵盖多个文档的 CustomUndoRecord。

似乎无法在同一代码中为两个文档创建 UndoRecord 列表 运行,无论 UndoRecord 列表是嵌套的还是连续创建和结束的。

UndoRecord.CustomRecordName 的帮助主题指出:

If custom undo records are nested within other custom undo records, this property specifies what string appears on the undo stack after all custom undo actions have completed. If multiple calls to the StartCustomRecord method are nested, the string specified by the first call will be returned by this property. If no action is active, the property returns an empty string.

如果调用不是嵌套的,而是连续的,在我的测试中,只有为第二个文档创建的第二个 CustomUndoRecord 的第一行代码存储在该名称下。在那之后,该命令似乎不再有效。在该点之后设置 StartCustomRecord 继续以相同的方式运行:只有一行执行存储在 UndoRecord 中,然后它再次变为非活动状态。我还尝试将 UndoRecord 代码放在一个单独的过程中 - 它没有任何区别。

结论:命令不支持您尝试执行的操作。

应该工作的是创建两个单独的 Word.Application 实例,每个文档在其自己的实例中打开。