热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使VBA代码一步撤消

如何解决《使VBA代码一步撤消》经验,为你挑选了1个好方法。

有没有办法让Word 2013宏的VBA代码作为块执行,所以如果我撤消它,它不会通过它执行的各个步骤撤消宏?我希望宏更像内置函数.

我有宏将光标移动到单词的末尾然后删除前三个字母.如果我撤消它,我会看到这三个字母中的每一个都会逐一重新出现.

我可能没有措辞这个权利.我想知道是否有代码来包装一个宏来让Word处理宏像一个内置的工作命令执行所有一次而不是执行一系列记录的步骤.然后,如果我运行一个宏并决定我不喜欢结果,我可以点击撤销,整个宏倒带和撤消.



1> 小智..:

使用Application.UndoRecord如下:

方法一:

Sub YourMacroName
    Dim objUndo As UndoRecord 
    'Declares the variable objUndo of the UndoRecord type
    Set objUndo = Application.UndoRecord 
    'sets the object to an actual item on the undo stack
    objUndo.StartCustomRecord ("Undo Stack Display Text")
    'Tells Word where to begin "recording" the undo record
    'This item also lets you set the text you want to appear in the undo drop-down in word.

        Selection.MoveRight Unit:=wdWord 
        'Moves to the end of the current word
        Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=True 
        'Selects the last three letters of the word
        Selection.Delete
        'Deletes the last three letters of the word
    objUndo.EndCustomRecord
    'Denotes the End of The Undo Record
End Sub

您可能还想查看Microsoft有关使用Application.UndoRecord属性的文章.

或者,正如@Matteo NNZ提到的那样,您可以一次删除这三个字符.为此,请使用以下代码.

方法二:

Sub YourMacroName
     Selection.MoveRight Unit:=wdWord 
    'Moves to the end of the current word
    Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=True 
    'Selects the last three letters of the word
    Selection.Delete
    'Deletes the last three letters of the word
End Sub

第二种方法的缺点是您无法在撤消下拉列表中控制项目的名称.

两种方法都受到限制,因为它们不会将光标恢复到原始位置.


推荐阅读
author-avatar
以真做假公馆
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有