If you want to write really clever and responsible VBA Macros, it's good to know that you have access to a whole bunch of document event listeners (triggered actions depending on a certain object or document state/change). In the following example, I'll show code to help you avoid 'Shapes will be deleted in a document by the User'.

This code can be pasted right away into any document VBA project (VBAProject "YourFileName"->CorelDRAW X5/X6 Objects>ThisDocument). If you want to reuse the code in your self-coded macros, you can replace the global variable "Document" with "GlobalMacroStorage". Be careful not to use this code in the predefined VBA project "GlobalMacros", however, because it could destabilize your Corel application.

All variables need to be declared explicitly:
Option Explicit
This declares the global variable "CurrentDoc".

Since we want to be able to activate or deactivate Event Listening on our decision, we need this variable to set or unset it with the active document; ' otherwise we could also use the predeclared variable "Document", ' but this would run our code permanently.

Private WithEvents CurrentDoc As Document
This variable is helpful to avoid constant event ripple fire such as with "CurrentDoc_ShapeMove":
Dim eventHasAlreadyFired As Boolean

The global variable "Document" is used to initialize our Event Listening, and is predeclared because this VBA code is attached to and saved with a document. When you write your own code, you have access to the variable "GlobalMacroStorage" instead.

The specific Event (in this case document "Open") is attached to the variable with an underscore.
Private Sub Document_Open()

So when the document is opened and the user agreed to run the attached Macro Code, the variable "CurrentDoc" will be set and the Event Listening will be initialized:
   bindEvents
End Sub

Set the variable CurrentDoc with this procedure to activate Event Listening on "CurrentDoc":
Sub bindEvents()

to fetch the active Document, we actually need to activate it:
    ActiveDocument.Activate

set "CurrentDoc" ONLY if it isn't already set:

If CurrentDoc Is Nothing Then: Set CurrentDoc = ActiveDocument
End Sub

The Event Listening will be stopped with this procedure because it unsets the global variable "CurrentDoc".
Sub unbindEvents()

unset the variable ONLY if it is explicitly set:
    If Not CurrentDoc Is Nothing Then: Set CurrentDoc = Nothing
End Sub

This procedure will be executed when the user closes the document.
Perfectly usable to release our Event Listening on "CurrentDoc" with "unbindEvents"
Private Sub Document_QueryClose(Cancel As Boolean)
Release Event Listening ONLY if the user REALLY closes the document. If he/she cancels to close, then the Event Listening will continue.
    If Cancel = False Then: unbindEvents
End Sub

Time to act: this procedure is executed when the user tries to delete any Shape in the document.
Private Sub CurrentDoc_ShapeDelete(ByVal Count As Long)

Only do this action once:
    If eventHasAlreadyFired = True Then: Exit Sub
Writes into the Immediate Window how many Shapes are tried to be deleted:
    Debug.Print Count & " Shape/s deleted."

Shows a Popup Message for the User:
    MsgBox "You are not allowed to delete any Shape in this Document!", vbExclamation

The Shape/s were deleted, so we "turn back time" with this line:
    CurrentDoc.Undo 1

Set our "Flag" to determine that this Event has already been fired now. If you want to permanently observe the "ShapeDelete" Event, you can omit this line.
    eventHasAlreadyFired = True
End Sub


Tip provided by Maurice Beumers, CorelDRAW  Master, Graphic designer and illustrator.

Note: This tip is available in 3 languages (English, French and German).