gpt4 book ai didi

vba - 一步到位

转载 作者:行者123 更新时间:2023-12-04 19:04:38 26 4
gpt4 key购买 nike

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

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

我可能没有正确地措辞。我想知道是否有代码可以包装一个宏,让 Word 将宏视为内置的 Work 命令,该命令只执行一次而不是执行一系列记录的步骤。然后,如果我运行一个宏并决定我不喜欢结果,我可以单击撤消,然后整个宏倒带和撤消。

最佳答案

使用 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 Property 的文章。 .
或者,正如@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
第二种方法的缺点是您无法在撤消下拉列表中控制项目的名称。
这两种方法的局限性在于它们不能将光标恢复到其原始位置。

关于vba - 一步到位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com