How to programaticly delete a shape when file closeing?

T

Tim Nash

I have a VB project created which imports a picture into a drawing. I need
some way to delete the picture when the file is closed. I have the name of
the imported file stored in a global variable, and i have the code to do the
delete.. what i don't have is an event to put it in. i was thinking the
document objects BeforeDocumentClose event would do the trick but it appears
the the document partialy closes before this event runs and i can not longer
manipulate shapes on the drawing by the time this event triggers. Any other
suggestions?
Thank you
 
A

Andy

I have a VB project created which imports a picture into a drawing. I need
some way to delete the picture when the file is closed. I have the name of
the imported file stored in a global variable, and i have the code to do the
delete.. what i don't have is an event to put it in. i was thinking the
document objects BeforeDocumentClose event would do the trick but it appears
the the document partialy closes before this event runs and i can not longer
manipulate shapes on the drawing by the time this event triggers. Any other
suggestions?
Thank you

I would try the QueryCancelDocumentClose event, this is early enough
to allow the close to be cancelled so should be able to change the
document still.
 
T

Tim Nash

Andy,
Thank you. Putting the code in the QueryCancelDocumentClose event works if
the document is closed, but generates and error if the application itself is
closed while the document is open. It generates a Run-time error
'-2032464741 (86db0c9b): an exception occured.
if i debug the code i can still refernce the objects when this error occurs,
I can get to the immediate window and return names of the active page and and
the shape in question, but i can't delete it.
I am storeing the shape name in a global variable when i bring it in, then
use that name to delete it when the time comes. Any help or suggestions are
appreciated.

Here is the code as i have it now.

Private Function Document_QueryCancelDocumentClose(ByVal doc As IVDocument)
As Boolean
If Len(sPictureName) > 0 Then
Application.ActivePage.Shapes(sPictureName).Delete
sPictureName = ""
End If
End Function
 
A

Andy

Andy,
Thank you. Putting the code in the QueryCancelDocumentClose  event works if
the document is closed, but generates and error if the application itselfis
closed while the document is open. It generates a Run-time error
'-2032464741 (86db0c9b): an exception occured.
if i debug the code i can still refernce the objects when this error occurs,
I can get to the immediate window and return names of the active page andand
the shape in question, but i can't delete it.
I am storeing the shape name in a global variable when i bring it in, then
use that name to delete it when the time comes.  Any help or suggestions are
appreciated.

Here is the code as i have it now.

Private Function Document_QueryCancelDocumentClose(ByVal doc As IVDocument)
As Boolean
If Len(sPictureName) > 0 Then
        Application.ActivePage.Shapes(sPictureName).Delete
        sPictureName = ""
End If
End Function

You could try detecting that it is the application being closed,
cancel the close and give the user a message telling them to close the
document first. The following example checks the scope to see if it is
an application exit (1016).

Private Function Document_QueryCancelDocumentClose( ByVal doc As
IVDocument) As Boolean

If Visio.Application.CurrentScope = 1016 Then
call msgbox("Please close the document before exiting")
Document_QueryCancelDocumentClose = true
else
'Delete shape
End If

End Function

Alternatively, could you delete your shape when the document is
opened.
 
C

Chris Roth [Visio MVP]

Hi Guys,

QueryCancelDocumentClose is expecting a result, but it looks like you
are only returning one for the true case?

In VB/VBA, add this:

QueryCancelDocumentClose = false

(if you want to allow the document to close, otherwise = true)

In VB.NET, add this:

Return false


Not sure if that is the problem, but probably a good idea to include
this anyway.

--
Hope this helps,

Chris Roth
Visio MVP


Visio Guy: Smart Graphics for Visual People

Articles: http://www.visguy.com
Shapes: http://www.visguy.com/shapes
Dev: http://www.visguy.com/category/development/
Forum: http://www.viguy.com/vgforum
 
A

Andy

Hi Guys,

QueryCancelDocumentClose is expecting a result, but it looks like you
are only returning one for the true case?

In VB/VBA, add this:

      QueryCancelDocumentClose = false

(if you want to allow the document to close, otherwise = true)

In VB.NET, add this:

      Return false

Not sure if that is the problem, but probably a good idea to include
this anyway.

--
Hope this helps,

Chris Roth
Visio MVP

Visio Guy: Smart Graphics for Visual People

  Articles:http://www.visguy.com
  Shapes:  http://www.visguy.com/shapes
  Dev:      http://www.visguy.com/category/development/
  Forum:    http://www.viguy.com/vgforum

For VBA/VB it is not necessary to always assign a return value, if not
assigned the return value will be the default initialization value for
the type (as defined in the language specification). So in this case
if not set true, by default it will be false.
 

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