Author Topic: Removing Unused Masters  (Read 3498 times)

0 Members and 1 Guest are viewing this topic.

MacGyver

  • Full Member
  • ***
  • Posts: 85
Removing Unused Masters
« on: January 19, 2017, 12: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!

chrisoft

  • Jr. Member
  • **
  • Posts: 14
Re: Removing Unused Masters
« Reply #1 on: January 19, 2017, 01: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
Code: [Select]
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

  • Full Member
  • ***
  • Posts: 85
Re: Removing Unused Masters
« Reply #2 on: January 19, 2017, 01: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.

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3457
  • Ideas Visio-lized into solutions
Re: Removing Unused Masters
« Reply #3 on: January 19, 2017, 02:47:50 PM »
When in doubt, try the macro recorder.  In this case you'll get the following code:
Code: [Select]
Sub Macro1()

    Application.ActiveDocument.RemoveHiddenInformation (visRHIMasters)

End Sub

Should be what you're after.

Wapperdude
Visio 2019 Pro

MacGyver

  • Full Member
  • ***
  • Posts: 85
Re: Removing Unused Masters
« Reply #4 on: January 20, 2017, 02: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?

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3457
  • Ideas Visio-lized into solutions
Re: Removing Unused Masters
« Reply #5 on: January 20, 2017, 03: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

Visio 2019 Pro

MacGyver

  • Full Member
  • ***
  • Posts: 85
Re: Removing Unused Masters
« Reply #6 on: January 20, 2017, 05: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:
Code: [Select]
Application.ActiveDocument.RemoveHiddenInformation (visRHIMasters) all the unused masters get deleted without warning message.

Think I might be SOL on this one.

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3457
  • Ideas Visio-lized into solutions
Re: Removing Unused Masters
« Reply #7 on: January 20, 2017, 06: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
Visio 2019 Pro

MacGyver

  • Full Member
  • ***
  • Posts: 85
Re: Removing Unused Masters
« Reply #8 on: January 20, 2017, 06: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.

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3457
  • Ideas Visio-lized into solutions
Re: Removing Unused Masters
« Reply #9 on: January 20, 2017, 07:22:42 PM »
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

  • Hero Member
  • *****
  • Posts: 2574
Re: Removing Unused Masters
« Reply #10 on: January 21, 2017, 08:47:45 AM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3457
  • Ideas Visio-lized into solutions
Re: Removing Unused Masters
« Reply #11 on: January 21, 2017, 10:33:54 AM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2747
    • Paul Herber's website
Re: Removing Unused Masters
« Reply #12 on: January 22, 2017, 12:30:49 PM »
A clean implementation would also check within any grouped shape.
Electronic and Electrical engineering, business and software stencils and applications for Visio -

http://www.paulherber.co.uk/

metuemre

  • Full Member
  • ***
  • Posts: 162
Re: Removing Unused Masters
« Reply #13 on: January 23, 2017, 01: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.