Visio Guy

Visio Discussions => Programming & Code => Topic started by: MacGyver on January 19, 2017, 05:54:59 PM

Title: Removing Unused Masters
Post by: MacGyver on January 19, 2017, 05:54:59 PM
Is there a way to determine if a document has unused masters?

I ask because I would like to build into my templates a check so that before user saves a document, the user is asked if they want to remove unused masters before saving. Ideally this question would only be asked if the document contains unused masters.  Don't want to be annoyed with this question prior to every save and don't want to do this without user approval.

Thanks!
Title: Re: Removing Unused Masters
Post by: chrisoft on January 19, 2017, 06:18:30 PM
Dear Mac,

Are you reffering to shapes dropped not coming from your own stencil ?
You have several ways for this.

First :
You could check if shape has a master by a boolean function including a loop over all shapes and check something like

If Not shp.master Is Nothing Then



If shape has no master this line will raise an error up, in the handling error, you could remove the said shape.

Second :
The alternative I have considered by my side
I have added in every shapes for each stencil, a section UsercellsDefined that contains reference. (in my case sql Id)
When I loop over all shapes I check if shape has a reference, in case of not, delete it.
Title: Re: Removing Unused Masters
Post by: MacGyver on January 19, 2017, 06:25:33 PM
I'm referring to masters that are stored in the document stencil that aren't being used in the document.
If you hit File > Info > Reduce file size, got to File Size Reduction tab, you'll see an option to remove unused master shapes. If you have a lot of unused master shapes, doing this can significantly decrease the size of your drawing file.
Title: Re: Removing Unused Masters
Post by: wapperdude on January 19, 2017, 07:47:50 PM
When in doubt, try the macro recorder.  In this case you'll get the following code:

Sub Macro1()

    Application.ActiveDocument.RemoveHiddenInformation (visRHIMasters)

End Sub


Should be what you're after.

Wapperdude
Title: Re: Removing Unused Masters
Post by: MacGyver on January 20, 2017, 07:45:56 PM
Wrapper thats the code I would use, but prior to running it, I'd just like to check if unused masters exist in the drawing. Is that possible?
Title: Re: Removing Unused Masters
Post by: wapperdude on January 20, 2017, 08:16:03 PM
That's what the code does.  Anything placed on a drawing page, connected or not, is considered used.

You can go to the Document stencil and iterate thru the shapes and try to delete.  If it is referenced by a shape on a drawing page, you'll get a warning.  I've not checked, but I think that's what the code does...intelligently.

Try it on some simple test cases to verify.

Wapperdude

Title: Re: Removing Unused Masters
Post by: MacGyver on January 20, 2017, 10:55:13 PM
If you try and delete a used master via code, it simply deletes the master, doesn't bother to give you a warning message.
If you try and delete a used master via typical Visio UI, you get the warning message before deleting the master.
If you use the code: Application.ActiveDocument.RemoveHiddenInformation (visRHIMasters) all the unused masters get deleted without warning message.

Think I might be SOL on this one.
Title: Re: Removing Unused Masters
Post by: wapperdude on January 20, 2017, 11:38:21 PM
But the point is, using the the UI in the doc stencil,indiscriminately looks at all masters, used and unused.  On the other hand, the code does just the unused.  So there is no warning.  I don't see the problem.

Wapperdude
Title: Re: Removing Unused Masters
Post by: MacGyver on January 20, 2017, 11:43:50 PM
99% of the time it would not be a problem but occasionally while I'm developing new tools, masters, patterns whatever, I'll have unused masters that I don't want to be deleted as I've likely spent a lot of time developing them. I might have a couple different versions of the same master but only one version will be used on the drawing.  I intend to put the master deleting code in the custom Add-in my team uses on a daily basis that would run prior to any document being saved.  Just don't want to regret not having a safety check for those rare times.
Title: Re: Removing Unused Masters
Post by: wapperdude on January 21, 2017, 12:22:42 AM
That being the case, you could write custom code.  But, how would you determine if a master should be kept or deleted?

Title: Re: Removing Unused Masters
Post by: Yacine on January 21, 2017, 01:47:45 PM
Probably collect all the used masters by checking the master of every shape in the document, then iterate through all the masters and check if they are in the collection previously made?
Title: Re: Removing Unused Masters
Post by: wapperdude on January 21, 2017, 03:33:54 PM
What occurs to me is that you have a working document for your development work, which only you have, and a distribution/published version that has the unused masters purged.  Then, the biggest issue is keeping both sync'd together.  This eliminates the problem of keeping certain unused masters.

Wapperdude
Title: Re: Removing Unused Masters
Post by: Paul Herber on January 22, 2017, 05:30:49 PM
A clean implementation would also check within any grouped shape.
Title: Re: Removing Unused Masters
Post by: metuemre on January 23, 2017, 06:35:46 AM
In one of my templates, I add a couple of masters to the drawing and delete them when the document is opened to make them available in the document stencil because some of the macros require them to be available.

To make sure that they will not be deleted by the user, I use Document.QueryCancelMasterDelete event.