(Solved) – Losing the Undo option when sending DELETE from within a Word VBA routine

  • by
(solved)-–-losing-the-undo-option-when-sending-delete-from-within-a-word-vba-routine

I have an application which allows the user to place data (using a SET field) within a document which describes information about the contents of the document. This information needs to be difficult for the average user of the document to see it, hence my use of the SET field and needs to be located near the location of the information in the document..

I want to trap the fact that a user may be copying, cutting or deleting this field in the normal course of editing the document. I have been able to code the copying and cutting operations so that I detect if the field is present in the user’s selection. (If it is there, I issue a warning and ask for permission to proceed.) I am currently having a problem with the Delete key and it occurs whether a field is present or not. Having set up a KeyBinding, the delete key is trapped and my routine is called. If it is a normal delete, I turn off the KeyBinding and use SENDKEYS to send a DELETE to delete the characters or selection. Finally, here is the problem, there is no Undo for this operation including when there is no field to delete. So basically, the Undo operation is no longer available for the delete key. Does anyone have an idea as to how to overcome this? I could, I suppose, populate the Undo stack but that would entail placing bookmarks all over the place to do so – not an appealing option. The KeyBinding:

 With Application
    If Not lOn Then 'clear the key binding
        'blnCatchBackspace = False
        FindKey(nKey).Clear
    Else 'apply the key binding
        'blnCatchBackspace = True
        .CustomizationContext = ActiveDocument
        .KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
            Command:="cRoutine", _
            KeyCode:=BuildKeyCode(wdKeyDelete)
    End If
End With

and the simple delete code:

Call ToggleCatch(False)  'Remove the delete key binding
SendKeys "{DELETE}"
DoEvents
Call ToggleCatch(True)   ' Turn the delete key binding on 

Any and all advice is greatly appreciated. Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *