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


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


Copies all the styles from the attached template to the document


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

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