Author Topic: [Tutorial]Preventing unwanted user input  (Read 4263 times)

0 Members and 1 Guest are viewing this topic.

Lars-Erik

  • Sr. Member
  • ****
  • Posts: 278
[Tutorial]Preventing unwanted user input
« on: April 24, 2008, 12:57:14 AM »
Introduction:

Ok , I'm working on a template that is going to be used by people that know little if anything at all about Visio.
To make sure they don't wreck there own files, I wanted to prevent certain inputs. Of course you can use the protect functions that come with Visio, which I did.
But to my surprise Visio lacks ( to the extend of my knowledge ) options to protect for example pages.
I'll share my VBA code that i wrote to protect some of the futures of my template that had to be protected.

As a side note:
I don't know if the way i wrote my VBA is the easiest way of doing things... But it works ;)
Also, cause we are going to be using code that runs on events, the code goes in the "ThisDocument" part of your file

So lets start off easy:

Prevent pages from being added:

This one speaks for it self, it will prevent ANY pages from being added.

Code
Private Sub Document_PageAdded(ByVal Page As IVPage)
'Revisions cause an error thus we catch it...
On Error GoTo A
'Prefent pages from being added
Page.Delete (Page.Index)
'When we try to add revisions we dont want to get an error, we want to do nothing...
A:
End Sub

Basically it looks for the event thats called when a page is added, once it finds it, it deletes the added page.

So the counterpart of this would be, you guessed it

Prevent pages from being deleted

A little more tricky in my opinion, as far as i could find out trying to prevent the deletion of a page in "BeforePageDelete" is already to late.
All you can do at that point is call a messagebox or maybe recreate the page (not with the above macro in place, nor will it have your shapes on it)
After some google searching I found that Visio has an event that get started when a page is deleted, this one will check if any of its sub event want to cancel the deletion event. so:

Code
Private Function Document_QueryCancelPageDelete(ByVal vsoPage As Visio.IVPage) As Boolean
'Function is ran when a page delete command is given
'When True is returned the deletion will be canceled
Document_QueryCancelPageDelete = True
'Always return true so no pages can be deleted
End Function

If we just force it to return true, it will always cancel the page deletion.

Prevent page renaming

It refuses to post this part ??? so here goes:



the code can be found here
You will have to alter the "1" where it gets the page name, 1 means two from the top ( 0 is the top one )line of the user data section, "2" one more down etc.
It should look something like this:



Prevent page reordering:

Fairly easy one, also build for my two page template but can be easily expanded. Because it checks for page names you will have to prevent them from being changed.
In my case the combination of the last two macro's work together to prevent people messing up my template. Again not one of my best macros as it is run more often then needed. Maybe someone else knows a nicer way of doing this. It doesn't seem to slow down my Visio though. Maybe noticeable on slow computers?

Code
Private Sub Document_DocumentChanged(ByVal doc As IVDocument)
'Document has been changed, make sure page order has not been changed.
ActiveDocument.Pages.Item("PFD").Index = 1
ActiveDocument.Pages.Item("P&ID").Index = 2
'Set the page order as it should be just to be sure
End Sub

If you don't want to lock your page names but do want to lock the page order this wont work, in that case you will have to rewrite this macro to act somewhat like the name protection macro shown above. Using the page properties to get the page oder information and then set it.

Thats all that I have at the moment. Hope this helps.

- Lars-Erik

<Edit>
- Revisions in Visio seem to call the "page added" event and caused an error with the macro preventing pages from being added. To make sure we dont lose Visio flexibility I've added an on Error that will prevent nasty errors when you try to add revisions. It will still prevent normal pages from being added.
</Edit>
« Last Edit: April 28, 2008, 08:36:16 AM by Lars-Erik »