Hi all,
In case someone else is struggling as I was, I found some sample code
to resolve this issue:
' clsQueryCancelPageDeleteEvent / QueryCancelPageDeleteEvent.cls
' Copyright (c) Microsoft Corporation. All rights reserved.
'
' Abstract - This class shows how to use the document's
' QueryCancelPageDelete event and related page deletion
' events. It also shows the sequence in which these events
' are fired.
Private WithEvents mvsoDocument As Visio.Document
Private mblnDocumentOpen As Boolean
Private Sub mvsoDocument_BeforeDocumentClose( _
ByVal vsoDoc As Visio.IVDocument)
' mvsoDocument_BeforeDocumentClose
'
' Abstract - This event gets fired before the
' the document closes. This class handles
' the BeforeDocumentClose event by updating
' an internal member variable that indicates if
' the document is open.
'
' Parameters
' vsoDoc Specifies the Document object,
' which in this case references
' the same document as
' mvsoDocument.
'
mblnDocumentOpen = False
End Sub
Private Sub mvsoDocument_BeforePageDelete( _
ByVal vsoPage As Visio.IVPage)
' mvsoDocument_BeforePageDelete
'
' Abstract - This event gets called just before the
' page is deleted and after the QueryCancelPageDelete
' event. The page object still exists when this
' procedure is called but it will be deleted as
' soon as all the BeforePageDelete event handlers
' return (there may be multiple clients monitoring
' this event).
'
' Note: If your solution needs to modify
' Visio objects as a result of a page deletion,
' it should do so in a BeforePageDelete event
' handler.
'
' Parameters
' vsoPage Specifies the page that is about to
' get deleted
'
Dim strMessage As String
strMessage = "Document_BeforePageDelete is called " _
& "for the page " & vsoPage.Name & ". " _
& "This event gets called after " _
& "QueryCancelPageDelete."
If (vsoPage.Application.AlertResponse = 0) Then
MsgBox strMessage
Else
Debug.Print strMessage
End If
End Sub
Private Sub mvsoDocument_PageDeleteCanceled( _
ByVal vsoPage As Visio.IVPage)
' mvsoDocument_PageDeleteCanceled
'
' Abstract - This event handler gets called by Visio after the
' QueryCancelPageDelete event. It gets called only if a handler for
' the QueryCancelPageDelete event in a client solution cancels the
' deletion of the page.
' Note: The QueryCancelPageDelete in this class
' displays a message box that allows the user to decide if page
' deletion should be canceled. The PageDeleteCanceled
' will always be called if "Cancel" is clicked in that message
' box. This procedure may also be called if "OK" is clicked in
' that message box, but only if another client or class cancels
' the delete in response to the QueryCancelPageDelete event.
' Parameters
' vsoPage Specifies the page that will not be deleted.
'
'
Dim strMessage As String
strMessage = "Document_PageDeleteCanceled is called for " _
& "page " & vsoPage.Name & ". " _
& "This event gets called whenever " _
& "an event handler for QueryCancelPageDelete " _
& "cancels the delete."
If (vsoPage.Application.AlertResponse = 0) Then
MsgBox strMessage
Else
Debug.Print strMessage
End If
End Sub
Private Function mvsoDocument_QueryCancelPageDelete( _
ByVal vsoPage As Visio.IVPage) As Boolean
' mvsoDocument_QueryCancelPageDelete
'
' Abstract - This event gets called whenever a user tries
' to delete a page in mvsoDocument through Visio's user
' interface. It will not be called when pages are
' deleted programatically.
'
' QueryCancel event handlers can access values in Visio
' objects but should not try to delete or modify Visio
' objects. Visio will usually throw an exception when
' QueryCancel event handlers try to modify Visio objects
' (for example, if the handler tries delete an object or
' a set a cell value).
' If your solution needs to make changes in Visio objects
' before a page is deleted, it should make those changes
' in the BeforePageDelete event handler.
'
'
' Parameters
' vsoPage Specifies the page that the user wants to
' delete.
Dim blnDelete As Boolean
If vsoPage.Application.AlertResponse = 0 Then
blnDelete = (MsgBox("Do you want to delete the page?", _
vbOKCancel) = vbOK)
Else
blnDelete = (vsoPage.Application.AlertResponse = vbOK)
End If
If blnDelete Then
' The user pressed "OK". Continue deletion of
' the page. Now the BeforePageDelete event may fire.
mvsoDocument_QueryCancelPageDelete = False
Else
' The user pressed "Cancel". Terminate deletion
' of the page.
' Now the PageDeleteCanceled event will fire.
mvsoDocument_QueryCancelPageDelete = True
End If
End Function
Public Sub SetDocument(vsoDoc As Visio.Document)
' SetDocument
'
' Abstract - This procedure sets the private member of
' the class to vsoDoc. It will only accept this
' document object if it is not already watching an
' open document.
'
' Parameters
' vsoDoc Specifies the Document object to be
' monitored by this class.
On Error GoTo SetDocument_Err
' Set the mvsoDocument only if the previous
' document is closed.
If (mblnDocumentOpen = False) Then
Set mvsoDocument = vsoDoc
mblnDocumentOpen = True
End If
Exit Sub
SetDocument_Err:
Debug.Print Err.Description
End Sub
Private Sub Class_Initialize()
' Class_Initialize
'
' Abstract - This procedure initializes the
' mblnDocumentOpen member variable which
' indicates if this class is monitoring
' events for an open Visio Document. Since
' the class has no references to Visio yet,
' set the value to False.
mblnDocumentOpen = False
End Sub
Good luck coding with Visio.