News:

BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

Removing Unused Masters

Started by MacGyver, January 19, 2017, 05:54:59 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MacGyver

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!

chrisoft

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.

MacGyver

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.

wapperdude

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
Visio 2019 Pro

MacGyver

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?

wapperdude

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

Visio 2019 Pro

MacGyver

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.

wapperdude

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
Visio 2019 Pro

MacGyver

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.

wapperdude

That being the case, you could write custom code.  But, how would you determine if a master should be kept or deleted?

Visio 2019 Pro

Yacine

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?
Yacine

wapperdude

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
Visio 2019 Pro

Paul Herber

A clean implementation would also check within any grouped shape.
Electronic and Electrical engineering, business and software stencils for Visio -

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

metuemre

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.

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 380 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1247KB.
Tokens: post-login.
Cache hits: 13: 0.00150s for 26,740 bytes (show)
Cache misses: 3: (show)
Queries used: 17.

[Show Queries]