Master Layers
Master Layers are only located on the MasterPage.
The Master Page is referred to as Page(0) or MasterPage
'The Master Page is page number 0.
Dim DOC_PAGE As Page
Set DOC_PAGE = ActiveDocument.Pages(0)
'Or
Set DOC_PAGE = ActiveDocument.MasterPage
A Master Page is a document page that can contain layers that can be visible on any page.
There are 5 types of layers that can be on the Master Page. They are;
Desktop Layer
Grid Layer - This displays a grid on potentially every page.
Guide Layer - This displays guides that can appear on every page.
Special Layer - I do not know what type of layer this is.
Master Layer - On this you place objects that potentially can appear on all pages.
A layer on the Master Page can appear on every page of a document.
If you manually create a MasterLayer you have 3 options.
Either the MasterLayer appears on ever page or on every odd or even page.
If you create a MasterLayer with VBA the MasterLayer will appear on every page by default.
On each individual page you can then turn the visibility of a layer on the Master Page on and off.
That way you can have different MasterLayers for every odd or even page or alternatively the start of every chapter etc.
Any layer that is on the master page is a Master but not necessarily a MasterLayer.
The layer can be any of the 5 types of layers mentioned earlier.
'Here layer(1) if it exists on the Master Page is always a Master. It is not necessarily a MasterLayer.
ThisDocument.Pages(0).Layers(1).Master = True
To Create a MasterLayer
MasterLayers created with VBA show as (all pages) type in Object Manager.
Even if you alter the visibility of a MasterLayer so that it shows on only odd pages it will still show in Object Manager as (all pages) type.
If you create a Master Layer with VBA whilst Object Manager is open and displaying All Pages, Layers & Objects then the new Master layer will not be visible in Object Manager.
However if Object Manager was displaying Current Page, Layers Only when the Master Layer was created then the Master Layer will be visible in All Pages, Layers & Object in Object Manager.
You can close and reopen the file or better still overcome this by updating the screen by useing Application.Refresh
Dim ly As Layer
'This next line creates and names the MasterLayer Mlayer.
Set ly = ActiveDocument.MasterPage.CreateLayer("MLayer")
Set ly = Nothing
Application.Refresh
Here
ThisDocument.Pages(1).Layers
refers to only the layers on page 1 even if they are hidden but not to any of the layers on the MasterPage.
ThisDocument.Pages(1).AllLayers
refers to all layers that can be shown on Page 1 including all MasterPage layers even if some of the layers including the MasterLayers are not visible.
There is no direct command to determine whether a layer is a MasterLayer.
However there are commands to determine if a layer on the MasterPage is one of the other 4 layers on a MasterPage.
To delete a MasterLayer.
If the Master Layer name is unknown you could examine every layer on the Master Page.
Dim ly As Layer
For Each ly In ActiveDocument.Pages(0).Layers
' By difference if the layer on the MasterPage is neither a DesktopLayer, GridLayer, GuidesLayer or a SpecialLayer it must be a MasterLayer.
If ly.IsDesktopLayer = False And ly.IsGridLayer = False And ly.IsGuidesLayer = False And ly.IsSpecialLayer = False Then
ly.Delete
End If
Next ly
If the Master Layer name is unknown you could examine every potentially visible layer on a Page.
Dim ly As Layer
For Each ly In ActiveDocument.Pages(2).AllLayers
'Any layer on the Master Page returns ly.Master = True such as Desktop Layer, Grid Layer, Guide Layer, Special Layer & Master Layer
'Remember that each page can have its own Guide Layer.
If ly.Master = True And ly.IsDesktopLayer = False And ly.IsGridLayer = False And ly.IsGuidesLayer = False And ly.IsSpecialLayer = False Then
ly.Delete
End If
Next ly
By default a new document has a layer called Layer 1 on page 1 and if you create a MasterLayer it will also be called Layer 1 by default.
It is a lot easier to rename all MasterLayers so that all layers have unique names.
If the Master Layer's name is known you can examine the Master Page.
Dim ly As Layer
'Look at only the layers on the master page.
For Each ly In ActiveDocument.Pages(0).Layers
If ly.Name = "MLayer 1" Then
ly.Delete
End If
Next ly
Alternatively if the Master Layer's name is known and is unique you can examine a page other than the Master Page.
Dim ly As Layer
'Look at all the layers that could appear on a page.
For Each ly In ActiveDocument.Pages(1).AllLayers
If ly.Name = "MLayer 1" Then
ly.Delete
End If
Next ly
2018_06_13
|