在文本框或组合框中撤消和恢复操作

以下示例演示如何在 TextBoxComboBox 的文本区域中撤消或重做文本编辑。 此示例检查是否可进行撤消或恢复操作,然后执行相应的操作。 此示例使用 CanUndoCanRedo 属性以及 UndoActionRedoAction 方法。

若要使用本示例,请将此示例代码复制到窗体的"脚本编辑器"中。 若要运行本代码,需要打开该窗体,以便激活 Open 事件。 确保该窗体包含:

  • 一个名为"TextBox1"的 TextBox

  • 一个名为"ComboBox1"的 ComboBox

  • 两个名称分别为"CommandButton1"和"CommandButton2"的 CommandButton 控件。

Dim UserForm1 
 
Sub CommandButton1_Click() 
 If UserForm1.CanUndo = True Then 
 UserForm1.UndoAction 
 MsgBox "Undid IT" 
 Else 
 MsgBox "No undo performed." 
 End If 
End Sub 
 
Sub CommandButton2_Click() 
 If UserForm1.CanRedo = True Then 
 UserForm1.RedoAction 
 MsgBox "Redid IT" 
 Else 
 MsgBox "No redo performed." 
 End If 
End Sub 
 
Sub Item_Open() 
 Set UserForm1 = Item.GetInspector.ModifiedFormPages("P.2") 
 Set TextBox1 = UserForm1.Controls("TextBox1") 
 Set ComboBox1 = UserForm1.Controls("ComboBox1") 
 Set CommandButton1 = UserForm1.Controls("CommandButton1") 
 Set CommandButton2 = UserForm1.Controls("CommandButton2") 
 
 TextBox1.Text = "Type your text here." 
 
 ComboBox1.ColumnCount = 3 
 ComboBox1.AddItem "Choice 1, column 1" 
 ComboBox1.List(0, 1) = "Choice 1, column 2" 
 ComboBox1.List(0, 2) = "Choice 1, column 3" 
 
 CommandButton1.Caption = "Undo" 
 CommandButton2.Caption = "Redo" 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。