Accessing Styles


Document Styles Collection

The Styles collection is available from the Document object. It is not available from the Template object.



Modifying a style within a Template

The Styles collection is available from the Document object. It is not available from the Template object.
If you want to modify styles in a template, use the OpenAsDocument method to open the template as a document


The following changes the formatting of Heading 1 in the template attached to this document

With ActiveDocument.AttachedTemplate.OpenAsDocument 
   .Styles(wdheading1).Font.Name = "Arial"
   .Close SaveChanges:=wdSaveOptions.wdsavechanges
End With


OrganizerCopy method

You can use this method to copy styles between documents and templates
Copies the specified AutoText entry, toolbar, style, or macro project item from the source document or template to the destination document or template.


Application.OrganizerCopy Source:=ActiveDocument.Name, _ 
                          Destination:="C:\Templates\Template1.dot", _
                          Name:="SubText", _
                          Object:=wdOrganizerObject.wdOrganizerObjectStyles

Source - String. The document or template file name that contains the item you want to copy.
Destination - String. The document or template file name to which you want to copy an item.
Name - String. The name of the AutoText entry, toolbar, style, or macro you want to copy.
Object - WdOrganizerObject. The kind of item you want to copy.


If the style named "SubText" exists in the active document, this example copies the style to C:\Templates\Template1.dot.

Dim styleLoop As Style 

For Each styleLoop In ActiveDocument.Styles
    If (styleLoop = "SubText") Then
        Application.OrganizerCopy Source:=ActiveDocument.Name, _
                                  Destination:="C:\Templates\Template1.dot", _
                                  Name:="SubText", _
                                  Object:=wdOrganizerObject.wdOrganizerObjectStyles
    End If
Next styleLoop

This example copies all the AutoText entries in the template attached to the active document to the Normal template.

Dim atEntry As AutoTextEntry 

For Each atEntry In ActiveDocument.AttachedTemplate.AutoTextEntries
    Application.OrganizerCopy.Source:=ActiveDocument.AttachedTemplate.FullName, _
                              Destination:=NormalTemplate.FullName, _
                              Name:=atEntry.Name, _
                              Object:=wdOrganizerObject.wdOrganizerObjectAutoText
Next atEntry


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext