VBA - Formatting


Obtains a collection of all the font names that are currently available

Dim oFontName As FontNames 
oFontNames = Application.FontNames
oFontNames = Application.PortraitFontNames
oFontNames = Application.LandscapeFontNames

Note the loop variable must be either an object variable or a variable of type Variant
It cannot be a string variable, as would otherwise be appropriate here.

Dim oFontName As Variant 
For Each oFontName In Application.FontNames
   Selection.InsertAfter objFontName.Name & vbCrLf
Next oFontName

Dim oFont as Font 
Set oFont = ActiveWindow.Selection.Font

Difference between Text and FormattedText

objRange.Text = the objects unformatted text

sText = oRange.Text 

objRange.FormattedText - returns a range object that represents the text and the formatting

oRange = obAnotherRange.FormattedText 

The FormattedText property has a special use and that is to transfer text and formatting from one range to another


Dim oRange As Range 
oRange = ActiveDocument.Words(2)

Clear Formatting


wdUndefined and wdToggle

This value cannot be set as a value but might be returned when a Range contains several different types of formatting.
Lets imagine that the first paragraph in a document contains both bold and not bold text.
This code will change all the text to bold when there is a mixture of bold and not bold and toggle the bold when it either all bold or all not bold.

Dim oFont as Font 
Set oFont = ActiveDocument.Paragraphs(1).Range.Font
If (oFont.Bold = wdConstants.wdUndefined) Then
   oFont.Bold = True
Else
   oFont.Bold = wdConstants.wdToggle
End If

Dim oFont As Font 
Set oFont = New Font
oFont.Name = "Arial"
oFont.Bold = -1 (True) / 0 (False)
oFont.Italic = True / False
oFont.Size = 12
oFont.Color = wdConstants.wdColorDarkYellow
oFont.TextColor.RGB = RGB(50,50,50)
oFont.Underline = wdUnderline.wdUnderlineSingle
oFont.UnderlineColor = wdColor.wdColorAutomatic

Effects

Dim oFont As Font 
Set oFont = New Font
      With oFont
         .StrikeThrough = True | False | wdConstants.wdToggle
         .DoubleStrikeThrough = True | False
'Setting the Superscript property to True automatically sets the Subscript property to False, and vice versa.
         .Superscript = True | False
         .Subscript = True | False
         .Shadow = True | False
         .Outline = True | False
         .Emboss = True | False
         .Engrave = True | False
         .SmallCaps = True
         .AllCaps = True | False
         .Hidden = True | False
      End With

oFont.Spacing = 0
oFont.Scaling = 100
oFont.Position = 0
oFont.Kerning = 0
oFont.Animation = wdAnimation.wdAnimationNone


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