VBA Code


Style Object

A style represents either a built-in style or a user defined style.


Get the Column Number of the selection

This only works for selections, not for ranges.

With Dialogs(wdDialogFormatColumns) 
   Debug.Print . ColumnNo
End With



Styles Collection

You can use the Styles property to return the Styles collection.

Dim objStyle As Style 
For Each objStyle In ActiveDocument.Styles

Next objStyle

If you want to modify styles in a template, use the OpenAsDocument method to open the template as a document
Use Styles(index), where index is a style name, a wdBuiltinStyle constant or index number


Dim oStyle As Style 
ActiveDocument.Styles("Normal") = oStyle;

Dim objStyle As Style 
Set objStyle = ActiveDocument.Styles("Color")

If you are using the style name it must match the spelling and spacing of the style name exactly, but not necessarily its capitalization.


Dim objStyle As Style 
Set objStyle = ActiveDocument.Styles(wdBuiltInStyle.wdStyleNormal)

Dim objStyle As Style 
Set objStyle = ActiveDocument.Styles(1)

The style index number represents the position of the style in the alphabetically sorted list of style names.
Note that Styles(1) is the first style in the alphabetic list.


Description Property


objStyle.Description 


Default Property

In VBA the default property is NameLocal ??

sName = objStyle 
sName = objStyle.NameLocal

Built-in Property



InUse Property

This property does not tell you whether a style is currently being used but rather tells you if the style is available to be used.
Built-in styles that have not been modified (or applied to any text) in this document will have this property set to False.
User defined styles will always have this property set to True.


Public Sub ListStylesCurrentlyAppliedToText() 
Dim objstyle As Style
Dim sstylelist As String
   sstylelist = "Styles in use:" & vbCr

   For Each objstyle In ActiveDocument.Styles
       If objstyle.InUse = True Then
           With ActiveDocument.Content.Find
               .ClearFormatting
               .Text = ""
               .Style = objstyle
               .Execute Format:=True
               If .Found = True Then
                   sstylelist = sstylelist & objstyle.NameLocal & vbCr
               End If
           End With
       End If
   Next objstyle
   Call MsgBox(sstylelist)
End Sub


BaseStyle Property


ActiveDocument.Styles(1).BaseStyle 


Copies all the styles from the attached template to the document

objDocument.UpdateStyles 

Dim objStylesCol As Styles 
   Set objStylesCol = objDocument.Styles


Do not rely on the default members !!!

Dim objStyle As Word.Style 
   objStyle.NameLocal = "Style Name"


It is possible to redefine a style based on changes that are made to a portion of text that has been formatted with that style.

objStyle.AutomaticallyUpdate = True | False 

Every style object of type wdStyleTypeParagraph has a ParagraphFormat object that represents the paragraph form settings for that style



If (objParagraph.Format.Style Like "Heading [0-9]") Then 
End If


VBA - 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

VBA - Creating Styles


Styles.Add Method

Use the Add method to create a new user-defined style and add it to the Styles collection.

ActiveDocument.Styles.Add Name:="New Style 1", _ 
                          Type:=wdStyleType.wdStyleTypeParagraph


Paragragh Styles

This example creates a new paragraph style called "New Paragraph Style 1"

Dim objStyle As Style 
Set objStyle = ActiveDocument.Styles.Add(Name:="New Paragraph Style 1", _
                                         Type:=wdStyleType.wdStylesParagraph)

With objStyle
   .Font.Bold
   .Font.Italic
   .Font.Name
   .Font.Size = 14
End With


Character Styles

This example creates a new character style called "New Character Style 1"

Dim objStyle As Style 
Set objStyle = ActiveDocument.Styles.Add(Name:="New Character Style 1", _
                                         Type:=wdStyleType.wdStyleTypeCharacter)



Styles based on existing styles

A style can be based on another style

Dim objStyle As Style 
Set objStyle = ActiveDocument.Styles.Add Name:="newstyle", _
                                         Type:=wdStyleType.wdStyleTypeParagraph
objStyle.BaseStyle = "Chapter"


A Style object includes the following attributes:

ActiveDocument.Styles("Normal").AutomaticallyUpdate = False 
ActiveDocument.Styles("Normal").BaseStyle = ""
ActiveDocument.Styles("Normal").NextParagraphStyle = "Normal"
ActiveDocument.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = False
  • Font

ActiveDocument.Styles("Normal").Font.Name = "Arial" 
ActiveDocument.Styles("Normal").Font.Size = 12
ActiveDocument.Styles("Normal").Font.Bold = False
ActiveDocument.Styles("Normal").Font.Italic = False
ActiveDocument.Styles("Normal").Font.Underline = False
ActiveDocument.Styles("Normal").Font.TextColor.RGB = RGB(200, 200, 200)
  • Paragraph

If (objStyle.Type = wdStyleType.wdStyleTypeParagraph) Then 
   objStyle.ParagraphFormat.Alignment = wdParagraphAlignment.wdAlignParagraphLeft
End If
  • Tabs

ActiveDocument.Styles("Normal").ParagraphFormat.TabStops 
  • Borders

ActiveDocument.Styles("Normal").Borders 
  • Language

ActiveDocument.Styles("Normal").LanguageID 
  • Frame

ActiveDocument.Styles("Normal").Frame 
  • Numbering

ActiveDocument.Styles("Normal").LinkToListTemplate _
      
                                ListTemplate:= ListGalleries(wdListGalleryType.wdBulletGallery).ListTemplates(1), _
                                ListLevelNumber:=1
  • Shortcut Keys


VBA - Applying Styles


To apply a style to a range, paragraph, or multiple paragraphs, set the Style property to a user-defined or built-in style name.

objRange.Style = Normal 
Selection.Range.Style = "My New Style"


F4 - Repeat

If you use this line then F4 cannot be used to repeat the style

Application.Selection.Style = sStyleName 

Instead use the wdDialogFormatStyle dialog box

Dim dlgStyle As Word.Dialog 
dlgStyle = Application.Dialogs(Word.wdWordDialog.wdDialogFormatStyle)
dlgStyle.Name = sStyleName
dlgStyle.Execute()

VBA - Updating Styles


UpdateStyles Method

You can use the UpdateStyles method to update the styles in the active document to match the style definitions in the attached template.

ActiveDocument.UpdateStyles 


UpdateStylesOnOpen Property

True is the styles are to be updated every time the document is opened

Documents("My Report.doc").UpdateStylesOnOpen = False 

VBA - Copying Styles


Document.CopyStylesFromTemplate


ActiveDocument.CopyStylesFromTemplate(template) 

template - the name of the template to use


When styles are copied across any existing styles in the document with the same name are redefined.
All existing styles are left unchanged.


VBA - Paragraph Character Linking


LinkStyle Property

Sets or returns a Variant that represents a link between a paragraph and a character style.
When a character style and a paragraph style are linked, the two styles take on the same character formatting.


This example creates and formats a new character style, and then it links the character style to the built-in heading style "Heading 1" so that the "Heading 1" style takes on the character formatting of the newly added style.

Sub LinkHeadStyle() 
    Dim styChar1 As Style

    Set styChar1 = ActiveDocument.Styles.Add(Name:="Heading 1 Characters", _
                                             Type:=wdStyleTypewdStyleTypeCharacter)
    With styChar1
        .Font.Name = "Verdana"
        .Font.Bold = True
        .Font.Shadow = True
        With .Font.Borders(1)
            .LineStyle = wdLineStyle.wdLineStyleDot
            .LineWidth = wdLineWidth.wdLineWidth300pt
            .Color = wdColor.wdColorDarkRed
        End With
    End With
    ActiveDocument.Styles("Heading 1").LinkStyle = ActiveDocument.Styles("Heading 1 Characters")

    With ActiveDocument.Content
        .InsertParagraphAfter
        .InsertAfter "New Linked Style"
        .Select
    End With

    Selection.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
    Selection.Style = ActiveDocument.Styles("Heading 1")
End Sub

VBA - Styles and Formatting Task Pane


Options

Keep track of formatting

Application.Options.FormatScanning = False 

Mark formatting inconsistencies

Application.Options.ShowFormatError = False 


Show Drop-Down

Sets or returns a WdShowFilter constant that represents the styles and formatting displayed in the Styles and Formatting task pane.

ActiveDocument.FormattingShowFilter = wdShowFilter.wdShowFilterStylesAll 

VBA - Printing Document Headings

Creates a new document with Heading XX style paragraphs from the active document

Public Sub PrintHeadings 
Dim objParagraph As Paragraph
Dim objRange As Range
Dim objDocumentA As Document
Dim objDocumentB As Document
Dim iLevel As Integer
Dim iMaxLevel As Integer

iMaxLevel = InputBox("Enter Maximum level for Heading style: ")
If (iMaxLevel = 0) Then Exit Sub

Set objDocumentA = Application.ActiveDocument
Set objDocumentB = Documents.Add(objDocumentA.AttachedTemplate.Name)
With objDocumentB.PageSetup
   .TopMargin - InchesToPoints(0.25)
   .BottomMargin - InchesToPoints(0.25)
   .LeftMargin - InchesToPoints(0.25)
   .RightMargin - InchesToPoints(0.25)
End With

Set objRange = objDocumentB.Range
For Each objParagraph In objDocumentA.Paragraphs
   iLevel = 0
   If objParagraph.Format.Style Like "Heading [0-9]" Then
      iLevel = Val(Mid(objParagraph.Format.Style,8))
      If (iLevel > 0) And (iLevel <- iMaxLevel) Then
         objRange.Collapse wdCollapseDirection.wdCollapseEnd
         objRange.Text = String(iLevel - 1), vbTab) & Format(iLevel) & ") " & objParagraph.Range.Text
     End If

' delete any annoying page breaks
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   With Selection.Find
      .Text = "^m"
      .Replacement.Text = ""
      .Forward = True
   End With
   Selection.Find.Execute Replace:=wdReplace.wdReplaceAll
End Sub

VBA - Used Styles


List all styles in a document

Public Sub ListAllStyles() 
Dim objstyle As Word.Style

   For Each objstyle In ActiveDocument.Styles
'Call MsgBox(objstyle.NameLocal)
      Selection.TypeText Text:=objstyle.NameLocal
      Selection.TypeParagraph
   Next objstyle
End Sub


Public Sub ListAllCustomStyles() 
Dim objstyle As Word.Style

   For Each objstyle In ActiveDocument.Styles
      If (objstyle.BuiltIn = False) Then
        Selection.TypeText Text:=objstyle.NameLocal
        Selection.TypeParagraph
      End If
   Next objstyle
End Sub

Creates a new document with Heading XX style paragraphs from the active document

Public Sub UsedStyles 
Dim arAllStyles() As String
Dim arUsedStyles() As String
Dim lTotalParagraphs As Long
Dim objParagraph As Paragraph
Dim sPreviousStyle As String
Dim iarraycount As Integer
Dim icount As Integer
Dim itotalusedstyles As Integer

lTotalParagraphs = ActiveDocument.Paragraphs.Count
ReDim arAllStyles(lTotalParagraphs)

For Each objParagraph In ActiveDocument.Paragraphs
   arAllStyles(iarraycount) = objParagraph.Style.Name
   iarraycount := iarraycount + 1
Next objParagraph

'sort the array alphabetically

ReDim arUsedStyles(lTotalParagraphs)

sPreviousStyle = ""
For icount = 1 to lTotalParagraphs
   If (arAllStyles(icount) <> sPreviousStyle)
      itotalusedstyles = itotalusedstyles + 1
      If (UBound(arUsedStyles) > itotalusedstyles) Then
         ReDim Preserve arUsedStyles(itotalusedstyles)
      End If

      arUsedStyles(itotalusedstyles) = arAllStyles(itotalusedstyles)
      sPreviousStyle = arAllStyles(itotalusedstyles)
   End If
Next icount

Document.Add
For icount = 1 To UBound(arStyles)
   Selection.InsertAfter arAllStyles(icount) & vbCr
   Selection.Collapse wdCollapseDirection.wdCollapseEnd
Next icount
End Sub

VBA - Style Sheet


Style Definition

Style type 
Style based on 
Style for following paragraph 
Add to template 
Automatically update 

Font

Font - Name 
Font - Style 
Font - Size 
Font - Colour 
Font - Underline Style 
Effects - Strikethrough 
Effects - Double Strikethrough 
Effects - Superscript 
Effects - Subscript 
Effects - Shadow 
Effects - Outline 
Effects - Emboss 
Effects - Engrave 
Effects - Small caps 
Effects - All caps 
Effects - Hidden 
Character - Scale 
Character - Scaling / By 
Character - Position / By 
Character - Kerning / Points 
Text Effects - Blinking Background 
Text Effects - Las Vegas Lights 
Text Effects - Marching Black Ants 
Text Effects - Marching Red Ants 
Text Effects - Shimmer 
Text Effects - Sparkle Text 

Paragraph

Alignment 
Outline Level 
Indentation - Left 
Indentation - Right / Special 
Spacing - Before 
Spacing - After 
Spacing - Line Spacing 
Pagination - Widow/Orphan 
Pagination - Keep line together 
Pagination - Keep with next 
Pagination - Page break before 
Pagination - Supress line numbers 
Pagination - Don't hyphenate 

Tabs

1 Alignment 
1 Leader 
2 Alignment 
2 Leader 

Borders

Borders - Setting 
Borders - Style 
Borders - Colour 
Borders - Width 
Borders - Apply To 
Shading - Fill 
Shading - Patterns 
Shading - Apply To 

Language

Other 

Frame

Text Wrapping 
Size - Width / At 
Size - Height / At 
Horizontal - Position 
Horizontal - Relative to 
Horizontal - Distance from text 
Vertical - Position 
Vertical - Relative to 
Vertical - Distance from text 
Move with text 
Lock anchor 

Numbering

Bulleted - Bullet Font 
Bulleted - Bullet Character 
Bulleted - Bullet Picture 
Bulleted - Position Indent at 
Bulleted - Text Tab space after 
Bulleted - Text Indent at 
Numbered - Bullet Font 
Numbered - Bullet Character 
Numbered - Bullet Picture 
Numbered - Position Indent at 
Numbered - Text Tab space after 
Numbered - Text Indent at 
Outline Numbered - Level 
Outline Numbered - Format 
Outline Numbered - Style 
Outline Numbered - Start at 
Outline Numbered - Font 
Outline Numbered - Number Position 
Outline Numbered - Aligned at 
Outline Numbered - Tab Space after 
Outline Numbered - Indent at 
Outline Numbered - Link Level to style 
Outline Numbered - Follow number with 
Outline Numbered - ListNum field list name 
Outline Numbered - Legal style numbering 
Outline Numbered - Restart numbering after 
Outline Numbered - Apply change to 
List Styles - Style 

Shortcut Key

Other 


VBA - BUG Visibility Property


objStyleVisibility Property

This property actually does the opposite to what it says.
ActiveDocument.Styles("").Visibility = True 'will hide the style
ActiveDocument.Styles("").Visibility = False 'will show the style



objStyleVisibility Property

Although this property should not really be used it can be useful to hide and show a list of styles in the Styles and Formatting task pane.
However if you run either of the following lines of code all the heading styles in your document will have a list style associated with them

ActiveDocument.Styles("Article / Section").Visibility = False 
ActiveDocument.Styles("Article / Section").Visibility = True

The code can be used in conjunction with any of the following "select styles to show" options "Recommended, In Use, In Current Document"



© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrev