How to Show Userform before deleting a shape?

Started by prasanna2j, October 21, 2011, 02:24:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

prasanna2j

Hi all,

Is there any way I can show a userform before I delete a shape from a page?

Basically, the userform will have a Yes/No command button, which will do stuff when clicked. Can I create Delete as an Event in the shapesheet?

Any other ideas are welcome.

Thanks.

Paul Herber

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

aledlund

In addition to Paul's reference you should check out the examples in the Visio SDK under event handling. Specifically the ShapeAdd\Delete event, and also the QueryCancelPageDelete Event (because it demonstrates the query process that Visio goes through). Be aware that these examples were put in around v2003 so some additional scopes have been added since then to support things like adding and deleting datagraphics and you may have to update the scope logic.
al

prasanna2j

Thank you for the replies Paul, Al!

From the SDK code samples, i found the following code, which does not respond to Alt+f8 or f5!


Private Sub mvsoPage_BeforeShapeDelete(ByVal vsoShape As Visio.IVShape)

' mvsoPage_BeforeShapeDelete
'
' Abstract - This procedure is called before a shape is
' deleted. The code demonstrates how to get the name of the
' Shape that is about to be deleted.
'
' Parameters
' vsoShape      Specifies the Shape that is about to be
'               deleted
'

    ' IsInScope with the visCmdUFEditCut parameter returns
    ' True if the shape is about to be deleted using the
    ' EditCut(Either through Ctrl-X or Menu action EditCut)
    ' action.
    If mvsoApp.IsInScope(visCmdUFEditCut) Then
        Debug.Print "Shape deleted using Edit Cut action. " _
            & "Universal name of the shape - " _
            & vsoShape.NameU
    Else
        Debug.Print "Shape is deleted. " _
            & "Universal name of the shape - " _
            & vsoShape.NameU
    End If
End Sub



WHat do I add further so that the code responds?

Regards
prasanna2j

Jumpy

The code should not respond to F5 because it should start automatically, when you delete a shape and not when you click a button or manually start a macro.

In your code modul you first need a reference variable to the page, that can react to events:
For example in the ThisDocument modul


Option Explicit

'Declare the variable:
Public WithEvents pg as Visio.Page

' "Fill" the variable:
Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
  Set pg = ActivePage
End Sub

Private Sub pg_BeforeShapeDelete(ByVal sh As Visio.Shape)
        Debug.Print "Shape is deleted. " _
            & "Universal name of the shape - " _
            & shp.NameU
End Sub


Look here at Chris article, about the smart way to fill variables at startup:
http://www.visguy.com/2007/05/25/run-vba-code-when-documents-open/

prasanna2j

Thanks Jumpy.

I added your code as a Class module (assuming that it needs to be one). I am getting an error now

" System Error & H80004005. Unspecified Error"

Any help on that?

prasanna2j

I found some help on the error

http://support.microsoft.com/kb/223152

It says "An MFC ActiveX control that subclasses any of the window classes implemented by Comctl32.dll causes the following error to be thrown from a Visual Basic 6.0 .exe."

Any connection between the code and the error??

Jumpy

#7
Can't help with that error. I didn't test the code as I only wrote it here in the forum's editor.
But as I mentioned: I would first try to place it in the ThisDocument VBA-modul of the drawing.

The RunModeEntered part must definitivly be in the This document modul. The rest can be placed in a class modul, but the class has to be created somewhere in the rest of the code...

prasanna2j

Thanks Jumpy.

Your code worked. But I did it from the dropdown options at the top of the vba window, by selecting the "Document" option in the left dropdown and "BeforeSelectionDelete" in the right.
I dont know if you intended to convey the same.
It seems to work none the less, and the error doesnt exist.

prasanna2j

Another favor, can we handle the "Enter" button event in the similar fashion?

As in, make the code do something after pressing "Enter" for a certain textbox?