Vb Applicatoion crashes after event QueryCancelSelectionDelete

V

visio_newbie

Hi all,

My developing environment:
MS Win 2000 sp4
VB6 SP6
Visio 2003 standard SP2

In VB, the code for the event is as follows:

Private Function vdcFlow_QueryCancelSelectionDelete(ByVal selection As
Visio.IVSelection) As Boolean
Dim RetVal As VbMsgBoxResult

If selection.Item(1).Type = visTypeGroup Then
RetVal = MsgBox("Are you sure you want to delete step " &
selection.Item(1).Name & "?", vbQuestion + vbOKCancel)
If RetVal = vbOK Then
'Proceed to delete the step from table Step
If Not DeleteStep(Val(selection.Item(1).NameU)) Then
MsgBox "Failed to delete the step.", vbExclamation
End If
Else
vdcFlow_QueryCancelSelectionDelete = True
End If
End If
End Function

No matter which button (OK or Cancel) is clicked, the application
crashes with error "Visual Basic has encountered a problem and needs to
close. We are sorry for the inconvenience...." after executing End
Function.

Can anybody shed some light on what I am doing wrong?
Thanks in advance.

Chu
 
J

JuneTheSecond

I tryed your code in VBA and VB .Net.
I dont know what is going inside
If Not DeleteStep(Val(selection.Item(1).NameU)) Then
MsgBox "Failed to delete the step.", vbExclamation
End If
, so I commented out the lines.
And there happens no problem.
Would you please check two points,
1. Inside the lines.
2. Did you release object instances, , Visio application and document,
when you close Form? For Example,
Set vdcFlow = Nothing
Set appVisio = Nothing
 
V

visio_newbie

Dear JuneTheSecond,

Thank you for your reply.
It does not matter what code is in the event. I mean the application
crashes even if I do not have any code in the event. The same thing
happens for all other QueryCancel events, like QueryCancelMasterDelete,
QueryCancelPageDelete, etc.
Any thoughts? Thanks.
Chu
 
M

Mark Nelson [MS]

You cannot delete a Visio shape while handling a "QueryCancel" or "Before"
event. Visio is in a modal state where you should not be making any changes
to the drawing. Treat Visio as read-only while handling these events. It
is possible that Visio is returning an error in this circumstance, and your
document is not catching it. (VBA has issues with error handling, so it may
not pass the error back to your code anyway. VB.Net should surface errors
properly.)

--
Mark Nelson
Office Graphics - Visio
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
 
V

visio_newbie

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top