【问题标题】:Undo all actions of a button VSTO撤消按钮 VSTO 的所有操作
【发布时间】:2021-05-05 04:02:01
【问题描述】:

我正在开发一个 MS Word 插件,所以我添加了一个按钮来触发一个功能:

private void button1_Click(object sender, RibbonControlEventArgs e) {
    // do some actions on a word document (text - formatting - ...) here.
}

我的问题是,当函数对 word 文档执行n 操作时,我必须单击撤消n 次才能撤消所有按钮操作。撤消 10 或 100 次以返回原始状态(文本 - 格式 - ... 等)是一种糟糕的用户体验。

有没有办法将所有按钮操作打包为撤消堆栈中的一个操作,以便我可以通过单击或Ctrl + z 撤消按钮效果?

重要提示:

对我有用的另一种方法是:

  • 打开 temp 文档。
  • 原始文档复制到temp文档。
  • temp 文档进行所有编辑(n 对其采取的操作)。
  • temp 文档复制回 original 文档(仅采取了一项操作,即 paste,因此我可以撤消它)。

这就是我在第二种方法中挣扎的原因:check here

【问题讨论】:

    标签: c# ms-word office-addins undo-redo


    【解决方案1】:

    您可以将所有内容包装在自定义撤消记录中 - 这样您只需撤消一次即可全部撤消。

    Visual Basic:

    Application.UndoRecord.StartCustomRecord "Title of undo-record here"
    Application.ScreenUpdating = False ' Optional, reduces screen flicker during operations.
    
    '--- Your code here
    
    Application.ScreenUpdating = True ' Optional, but required if set to false above
    Application.UndoRecord.EndCustomRecord
    
    ' After this, you can undo once to undo it all.
    

    C#:

    Application.UndoRecord.StartCustomRecord "Title of undo-record here";
    Application.ScreenUpdating = false; // Optional, reduces screen flicker during operations.
    
    //--- Your code here
    
    Application.ScreenUpdating = true; // Optional, but required if set to false above
    Application.UndoRecord.EndCustomRecord;
    
    // After this, you can undo once to undo it all.
    

    【讨论】:

    • 在 C# 中是否有等价物?
    • @AhmedHussein 在 C# 中也是一样的,只是在每行末尾加一个分号,对于 cmets 将 ' 替换为 // :-)
    【解决方案2】:

    这个 StackOverFlow 答案应该为您指明正确的方向。

    Save undo stack during macro run

    【讨论】:

    • 这不适用于某些类型的文档更改。例如,我无法恢复对文档文本执行一些正则表达式替换的按钮所执行的操作。
    猜你喜欢
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2011-02-24
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多