gpt4 book ai didi

.net - 我可以在 Word 或 Excel 中创建撤消事务吗? (VSTO)

转载 作者:行者123 更新时间:2023-12-02 03:29:20 25 4
gpt4 key购买 nike

我注意到 Project 2007 具有允许将可撤消的操作放置在单个堆栈项或“撤消事务”中的功能。 For example:

Application.OpenUndoTransaction "Create 6 tasks"
Dim i As Integer
For i = 1 To 6
ActiveProject.Tasks.Add "UndoMe " & i
Next
Application.CloseUndoTransaction

这意味着用户可以在一次撤消操作中撤消所有操作,而不是 6 次。

这在 Word 和/或 Excel 中实现会很棒,因为我正在 VSTO 中做一些同时进行多项更改的操作,如果用户必须单击“撤消”,这对用户来说会有点烦人如果他们犯了错误,就多说几次。尽管这些特定功能似乎不存在,但有人知道是否/如何以某种方式完成此操作?

最佳答案

您可以通过覆盖 VBA 中的撤消和重做命令例程来模拟 Word 中的事务行为(不过,我认为单独使用 VSTO 不可能覆盖内置 Word 命令)。通过添加书签来标记事务的开始,通过删除书签来标记事务的结束。

当调用undo时,我们检查事务标记书签是否存在并重复undo直到标记消失。重做的工作方式相同。该机制支持对文档内容所做的所有修改的事务撤消/重做。但是,为了允许撤消/重做对文档属性的修改,需要使用 SetCustomProp 宏来实现特殊机制。文档属性不应直接设置,而只能通过此宏设置。

更新:我忘了明确提及,这种方法仅适用于键盘快捷键和菜单命令,单击工具栏按钮仍然会执行单步撤消。因此,我们决定用自定义按钮替换工具栏按钮。该代码已在 Word 2003 中使用了相当长一段时间(尚未在 Word 2007 中进行测试,因此请做好意外惊喜的准备;)

Option Explicit

' string constants for Undo mechanism
Public Const BM_IN_MACRO As String = "_InMacro_"

Public Const BM_DOC_PROP_CHANGE As String = "_DocPropChange_"
Public Const BM_DOC_PROP_NAME As String = "_DocPropName_"
Public Const BM_DOC_PROP_OLD_VALUE As String = "_DocPropOldValue_"
Public Const BM_DOC_PROP_NEW_VALUE As String = "_DocPropNewValue_"

'-----------------------------------------------------------------------------------
' Procedure : EditUndo
' Purpose : Atomic undo of macros
' Note: This macro only catches the menu command and the keyboard shortcut,
' not the toolbar command
'-----------------------------------------------------------------------------------
Public Sub EditUndo() ' Catches Ctrl-Z

'On Error Resume Next
Dim bRefresh As Boolean
bRefresh = Application.ScreenUpdating
Application.ScreenUpdating = False

Do
If ActiveDocument.Bookmarks.Exists(BM_DOC_PROP_CHANGE) Then
Dim strPropName As String
Dim strOldValue As String

strPropName = ActiveDocument.Bookmarks(BM_DOC_PROP_NAME).Range.Text
strOldValue = ActiveDocument.Bookmarks(BM_DOC_PROP_OLD_VALUE).Range.Text
ActiveDocument.CustomDocumentProperties(strPropName).Value = strOldValue
End If

Loop While (ActiveDocument.Undo = True) _
And ActiveDocument.Bookmarks.Exists(BM_IN_MACRO)

Application.ScreenUpdating = bRefresh
End Sub

'-----------------------------------------------------------------------------------
' Procedure : EditRedo
' Purpose : Atomic redo of macros
' Note: This macro only catches the menu command and the keyboard shortcut,
' not the toolbar command
'-----------------------------------------------------------------------------------
Public Sub EditRedo() ' Catches Ctrl-Y

Dim bRefresh As Boolean
bRefresh = Application.ScreenUpdating
Application.ScreenUpdating = False

Do
If ActiveDocument.Bookmarks.Exists(BM_DOC_PROP_CHANGE) Then
Dim strPropName As String
Dim strNewValue As String

strPropName = ActiveDocument.Bookmarks(BM_DOC_PROP_NAME).Range.Text
strNewValue = ActiveDocument.Bookmarks(BM_DOC_PROP_NEW_VALUE).Range.Text
ActiveDocument.CustomDocumentProperties(strPropName).Value = strNewValue
End If

Loop While (ActiveDocument.Redo = True) _
And ActiveDocument.Bookmarks.Exists(BM_IN_MACRO)

Application.ScreenUpdating = bRefresh

End Sub

'-----------------------------------------------------------------------------------
' Procedure : SetCustomProp
' Purpose : Sets a custom document property
'-----------------------------------------------------------------------------------
Public Function SetCustomProp(oDoc As Document, strName As String, strValue As String)

Dim strOldValue As String

On Error GoTo existsAlready
strOldValue = ""
oDoc.CustomDocumentProperties.Add _
Name:=strName, LinkToContent:=False, Value:=Trim(strValue), _
Type:=msoPropertyTypeString
GoTo exitHere

existsAlready:
strOldValue = oDoc.CustomDocumentProperties(strName).Value
oDoc.CustomDocumentProperties(strName).Value = strValue

exitHere:
' support undo / redo of changes to the document properties
'On Error Resume Next
Dim bCalledWithoutUndoSupport As Boolean

If Not ActiveDocument.Bookmarks.Exists(BM_IN_MACRO) Then
ActiveDocument.Range.Bookmarks.Add BM_IN_MACRO, ActiveDocument.Range
bCalledWithoutUndoSupport = True
End If

Dim oRange As Range
Set oRange = ActiveDocument.Range

oRange.Collapse wdCollapseEnd
oRange.Text = " "
oRange.Bookmarks.Add "DocPropDummy_", oRange

oRange.Collapse wdCollapseEnd
oRange.Text = strName
oRange.Bookmarks.Add BM_DOC_PROP_NAME, oRange

oRange.Collapse wdCollapseEnd
oRange.Text = strOldValue
oRange.Bookmarks.Add BM_DOC_PROP_OLD_VALUE, oRange

oRange.Collapse wdCollapseEnd
oRange.Text = strValue
oRange.Bookmarks.Add BM_DOC_PROP_NEW_VALUE, oRange

oRange.Bookmarks.Add BM_DOC_PROP_CHANGE
ActiveDocument.Bookmarks(BM_DOC_PROP_CHANGE).Delete

Set oRange = ActiveDocument.Bookmarks(BM_DOC_PROP_NEW_VALUE).Range
ActiveDocument.Bookmarks(BM_DOC_PROP_NEW_VALUE).Delete
If Len(oRange.Text) > 0 Then oRange.Delete

Set oRange = ActiveDocument.Bookmarks(BM_DOC_PROP_OLD_VALUE).Range
ActiveDocument.Bookmarks(BM_DOC_PROP_OLD_VALUE).Delete
If Len(oRange.Text) > 0 Then oRange.Delete

Set oRange = ActiveDocument.Bookmarks(BM_DOC_PROP_NAME).Range
ActiveDocument.Bookmarks(BM_DOC_PROP_NAME).Delete
If Len(oRange.Text) > 0 Then oRange.Delete

Set oRange = ActiveDocument.Bookmarks("DocPropDummy_").Range
ActiveDocument.Bookmarks("DocPropDummy_").Delete
If Len(oRange.Text) > 0 Then oRange.Delete

If bCalledWithoutUndoSupport And ActiveDocument.Bookmarks.Exists(BM_IN_MACRO) Then
ActiveDocument.Bookmarks(BM_IN_MACRO).Delete
End If

End Function

'-----------------------------------------------------------------------------------
' Procedure : SampleUsage
' Purpose : Demonstrates a transaction
'-----------------------------------------------------------------------------------
Private Sub SampleUsage()

On Error Resume Next

' mark begin of transaction
ActiveDocument.Range.Bookmarks.Add BM_IN_MACRO

Selection.Text = "Hello World"
' do other stuff

' mark end of transaction
ActiveDocument.Bookmarks(BM_IN_MACRO).Delete

End Sub

关于.net - 我可以在 Word 或 Excel 中创建撤消事务吗? (VSTO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/661269/

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