Visio Guy

Visio Discussions => Programming & Code => Topic started by: SubPlanner on January 27, 2016, 01:57:48 PM

Title: How to protect a page from deletion [Solved]
Post by: SubPlanner on January 27, 2016, 01:57:48 PM
I have situations where I need to protect certain pages from deletion.

Here is the code:

Private Function Document_QueryCancelPageDelete(ByVal vsoPage As Visio.IVPage) As Boolean
Dim pg As Page
'Function is ran when a page delete command is given
'When True is returned the deletion will be canceled
For Each pg In ActiveDocument.Pages
'
If vsoPage.Name = "PageName" Then 'Name of page
   MsgBox "Page Deletion not Allowed", vbOKOnly + vbInformation, "Page Deletion Protector"
   Document_QueryCancelPageDelete = True
Exit Function

End If

Next pg
'Document_QueryCancelPageDelete = True
'Always return true so no pages can be deleted
End Function


This is the basic code that is generally used when protecting all pages within a Visio document from deletion.
Here I just added an "IF" function to only protect certain pages.

Cheers.
SubPlanner
Title: Re: How to protect a page from deletion [Solved]
Post by: Yacine on January 27, 2016, 02:09:18 PM
You will probably want to remove the for each loop. ;)
Title: Re: How to protect a page from deletion [Solved]
Post by: SubPlanner on January 27, 2016, 02:24:44 PM
Ya, I guess I left it hanging in there as a leftover.

still works good.

I do have a question,
How would I protect the page from being renamed?

SubPlanner
Title: Re: How to protect a page from deletion [Solved]
Post by: AndyW on January 27, 2016, 04:01:22 PM
You can't stop it, but you can handle it.

Whenever I set a page name I make sure to update the universal name (NameU) to match the name.

Then I catch the document PageChanged event and if NameU and Name don't match, then I change the Name back yo the NameU. It does mean you will have to provice a mechanism to rename the pages if you allow that.
Title: Re: How to protect a page from deletion [Solved]
Post by: AndyW on January 27, 2016, 04:03:09 PM
Also, rather than check for specific pages names, I would add users cells to your pages that you could check instead, makes it more general, e.g. User.NoPageDelete and User.NoPageRename
Title: Re: How to protect a page from deletion [Solved]
Post by: SubPlanner on January 27, 2016, 04:22:29 PM
I am still a bit of a novice when it comes to applying VBA code in Visio. (I have had to much time in Access)
What would the code structure look like to make this work.

Thanks for your help
"SbP"
Title: Re: How to protect a page from deletion [Solved]
Post by: Visisthebest on February 02, 2022, 09:32:36 AM
I double-checked and indeed there is no Protection shapesheet section on a page as far as I can see, so no option to block deletion that way.