VBA Code


Sections
Paragraphs
Sentences
Words
Characters


When modifying text it is not necessary to select the text before modifying it


activedocument.content.paragraphformat.space 2 


Dim objParagraph As Paragraph 
Set objParagraph = ActiveDocument.Paragraphs(2)
objParagraph.Alignment = wdParagraphAlignment.wdAlignParagraphLeft


Dim objRange As Range 
Set objRange = ActiveDocument.Paragraphs(2).Range
objRange.Font.Bold = True | False

Dim objFont As Font 
Set objFont = ActiveDocument.Paragraphs(2).Range.Font
objFont.Bold = True | False


objDocument.EmbedTrueTypeFonts = 

These two lines are equivalent

objDocument.Paragraphs(2).CloseUp 
objDocument.Paragraphs(2).SpaceBefore = 0

objDocument.Paragraphs(2).SpaceAfter = 0 

Dim objParagraphFormat 
objParagraphFormat = objParagraph.Format

The Range property returns a Range object that represents the portion of a document that is contained in the paragraph

objRange = objParagraph.Range 
objRange.Text = "some text"
objRange.Font.Name = "Arial"

All the properties and methods of the ParagraphFormat object are also properties and methods of the Paragraph object except the Duplicate property.




activedocument.range(start := mydoc.sentances(2).start, end := mydoc.sentances(4).end) 


Manipulates the currently selected paragraph

With Selection.Paragraphs(1).Range 
   .Font.Bold = False
End With


Get the Index number of the current paragraph

Define a range from the start of the document to the end of the first selected paragraph

Msgbox (ActiveDocument.Range(0,Selection.Paragraphs(1).Range.End).Paragraphs.Count) 

Selection.Sections(1) or Selection.Tables(1)




Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter 


Selection.ParagraphFormat.LeftIndent = InchesToPoints(2) 
Selection.ParagraphFormat.RightIndent = InchesToPoints(2)


Looping through all

Sub LoopAll 
Dim oCurrentParagraphs As Word.Range
Dim oParagraph As Word.Paragraph
Dim lParagraphNo As Long

   Application.Selection.HomeKey Unit:=WdUnits.wdStory, Extend:=WdMovementType.wdMove
   Application.Selection.Range.Select
   Do Until ((Selection.End = ActiveDocument.Content.End) Or _
             (Selection.End = ActiveDocument.Content.End - 1))
      Set oParagraph = Selection.Paragraphs(1)
      oParagraph.Range.Select

      Set oCurrentParagraphs = ActiveDocument.Range(Start:=0, End:=oParagraph.Range.End)
      lParagraphNo = oCurrentParagraphs.Paragraphs.Count

      If (Paragraph_DisplaysBullet(oParagraph.Range) = True) Then
         Debug.Print "Paragraph: " & lParagraphNo & " - has a bullet"
      End If
      
      Selection.MoveDown Unit:=WdUnits.wdParagraph, Count:=1, Extend:=WdMovementType.wdMove
   Loop
   
End Sub

VBA - Paragraphs Collection

The Paragraphs collection is read-only and represents all the paragraphs in the specified document, range or selection.


This is the paragraphs collection for the current selection

ActiveWindow.Selection.Paragraphs 

This is the paragraphs collection for the first section of the active document

ActiveDocument.Sections(1).Range.Paragraphs 

Paragraphs(Index) where index is the index number



Count Property

The Count property returns the total number of paragraphs


ActiveDocument.Paragraphs.Count 
ActiveDocument.Sections(1).Paragraphs.Count


Paragraph Object

This represents a single paragraph in a document, range or selection.


VBA - Paragraphs


Toggling the display of the Paragraph Marks

ActiveWindow.ActivePane.View.ShowAll = Not ActiveWindow.ActivePane.View.ShowAll 

Use the Format property to return the ParagraphFormat object for a paragraph or paragraphs.
The ParagraphFormat property returns the ParagraphFormat object for a selection, range, style, Find object, or Replacement object.


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 objParagraphFormat as ParagraphFormat 
Set objParagraphFormat = Selection.ParagraphFormat
If (objParagraphFormat.Hyphenation = wsConstants.wdUndefined) Then
   objParagraphFormat.Hyphenation = True
Else
   objParagraphFormat.Hyphenation = wsConstants.wdToggle
End If

ParagraphFormat Object


   With Selection.ParagraphFormat 
      .Alignment = wdParagraphAlignment.wdAlignParagraphLeft
      .AutoAdjustRightIndent = True | False 'automatically adjust the right indent for the specified paragraphs if you've specified a set number of characters per line
      .BaseLineAlignment = wdBaselineAlignment.wdBaselineAlignAuto 'represents the vertical position of fonts on a line
      .CharacterUnitFirstLineIndent = 0
      .CharacterUnitLeftIndent = 0
      .CharacterUnitRightIndent = 0
      .DisableLineHeightGrid = True | False
      .FirstLineIndent = CentimetersToPoints(-1) 'positive value for a first-line indent, negative value for a hanging indent
      .HangingPunctuation = True | False
      .Hyphenation = True | False
      .KeepTogether = True | False
      .KeepWithNext = True | False
      .LeftIndent = CentimetersToPoints(1) 'this value must be in points
      .LineSpacing = 12
      .LineSpacingRule = wdLineSpacing.wdLineSpaceAtLeast
      .LineUnitAfter = 0
      .LineUnitBefore = 0
      .NoLineNumber = True | False
      .OutlineLevel = wdOutlineLevel.wdOutlineLevelBodyText
      .PageBreakBefore = True | False
      .RightIndent = CentimetersToPoints(0)
      .SpaceAfter = CentimetersToPoints(1.5) 'this value must be in points
      .SpaceAfterAuto = True | False
      .SpaceBefore = CentimetersToPoints(1.5) 'this value must be in points
      .SpaceBeforeAuto = True | False
      .Style = wdBuiltinStyle.wdStyleHeading1
      .TabStops =
      .WidowControl = True | False
      .WordWrap = True | False
   End With

Dim objParagraphFormat2 As ParagraphFormat
Set objParagraphFormat2 = Selection.ParagraphFormat.Duplicate 'returns a read only object


Borders


Dim colBordersCollection As Borders 

Set colBordersCollection = Selection.ParagraphFormat.Borders

Selection.ParagraphFormat.Borders = colBordersCollection


Shading


Dim objShading As Shading 

Set objShading = Selection.ParagraphFormat.Shading

With Selection.ParagraphFormat.Shading
   .BackgroundPatternColor = wdColorBlue
   .BackgroundPatternColorIndex =
   .ForegroundPatternColor =
   .ForegroundPatternColorIndex =
   .Texture =
End With

This example sets Formatting Paragraphs for a range that includes the entire contents of MyDoc.doc. Paragraphs in this document are double-spaced and have a custom tab stop at 0.25 inch.

Set myRange = Documents("MyDoc.doc").Content 
With myRange.ParagraphFormat
    .Space2
    .TabStops.Add Position:=InchesToPoints(.25)
End With

With ActiveDocument.Content.Find 
    .ClearFormatting
    .ParagraphFormat.Space2
    .Replacement.ClearFormatting
    .Replacement.ParagraphFormat.Space15
    .Execute FindText:="", _
             ReplaceWith:="", _
             Replace:=wdReplace.wdReplaceAll
End With

This example modifies the Heading 2 style for the active document. Paragraphs formatted with this style are indented to the first tab stop and double-spaced.

With ActiveDocument.Styles(wdBuiltinStyle.wdStyleHeading2).ParagraphFormat 
    .TabIndent(1)
    .Space2
End With

With Application.ActivePresentation.Slides(2).Shapes(2) 
    With .TextFrame.TextRange2.ParagraphFormat
        .LineRuleWithin = msoTrue
        .SpaceWithin = 1.4
        .LineRuleBefore = msoTrue
        .SpaceBefore = 0.25
        .LineRuleAfter = msoTrue
        .SpaceAfter = 0.75
    End With
End With

ActiveDocument.Paragraphs(3).Format.Alignment = wdParagraphAlignment.wdAlignParagraphCenter 

You can use Visual Basic's New keyword to create a new, standalone ParagraphFormat object. The following example creates a ParagraphFormat object, sets some formatting properties for it, and then applies all of its properties to the first paragraph in the active document.

Dim objParagraphFormat As New ParagraphFormat 
objParagraphFormat.Alignment = wdParagraphAlignment.wdAlignParagraphCenter
objParagraphFormat.Borders.Enable = True
ActiveDocument.Paragraphs(1).Format = objParagraphFormat


Set objParagraphFormat = ActiveDocument.Paragraphs(1).Format.Duplicate 
myDup.LeftIndent = InchesToPoints(1)
Documents.Add
Selection.InsertAfter "This is a new paragraph."
Selection.Paragraphs.Format = objParagraphFormat

VBA - Inserting Paragraphs

You can use any of the following methods to add new paragraphs to a document:


Paragraphs.Add

Adds a new paragraph before the first paragraph in the current selection

ActiveWindow.Selection.Paragraphs.Add Range:=ActiveWindow.Selection.Paragraphs(1).Range 

Range.InsertParagraph

This replaces the specified range or selection with a new paragraph.
If you do not want to replace the range or selection you should collapse the range or selection first.

Set oRange = ActiveWindow.Selection.Paragraphs(1).Range.InsertParagraph 

This is equivalent to the following:
After this method has been called the oRange object represents the new paragraph.

Set oRange = ActiveWindow.Selection.Paragraphs(1).Range 
oRange.InsertParagraph

This inserts a new paragraph at the beginning of the active document.

Set oRange = ActiveDocument.Range(Start:=0, End:=0) 
oRange.InsertParagraph

This inserts a new paragraph at the end of the active document.

Set oRange = ActiveDocument.Content 
oRange.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
oRange.InsertParagraph
oRange.Move Unit:=wdUnits.wdParagraph, Count:=1

Range.InsertParagraphAfter

This inserts a new paragraph after the specified range or selection
After this method has been called the specified range or selection is expanded to include the new paragraph.


ActiveWindow.Selection.InsertParagraphAfter 

ActiveWindow.Selection.Paragraphs(1).Range.InsertParagraphAfter 

This inserts a paragraph at the end of the active document

ActiveDocument.Content.InsertParagraphAfter 

Range.InsertParagraphBefore

This inserts a new paragraph before the specified range or selection


ActiveWindow.Selection.Paragraphs(1).Range.InsertParagraphBefore 

This line is identical to the following line

ActiveWindow.Selection.Paragraphs.Add Range:=ActiveWindow.Selection.Paragraphs(1).Range 

VBA - Formatting Paragraphs


Selection.ParagraphFormat.LeftIndent = InchesToPoints(4) 
Selection.ParagraphFormat.RightIndent = InchesToPoints(4)

Selection.ParagraphFormat.LineSpacingRule = wdLineSpacing.wdLineSpaceExactly 
Selection.ParagraphFormat.LineSpacing = 20

Selection.ParagraphFormat.KeepTogether = True | False 
Selection.ParagraphFormat.KeepWithNext = True | False
Selection.ParagraphFormat.WidowControl = True | False

Selection.ParagraphFormat.SpaceAfter = 10 
Selection.ParagraphFormat.SpaceBefore = 20

objParagraph.LeftAlign = 20 

Paragraphs(1).Alignment = wdParagraphAlignment.wdAlignParagraphRight 

VBA - Tabs

For header/footer alignment tabs refer to Layout > VBA Code > Alignment Tabs


Tab Stops Collection

A tab stop object represents a tab stop
A tab stops collection is a collection of all the tabs stop objects.
Each of the Paragraph, ParagraphFormat and Paragraphs objects have a TabStops property which returns a tabstops collection.
The TabStops collection represents the custom and default tabs for a paragraph or group of paragraphs.
Tab stops are indexed numerically from left to right along the ruler.


Dim objTabStopsCol As TabStops 

Dim objTabStop As TabStop 
For Each objTabStop In Selection.Paragraphs.TabStops
      objTabStop.Position
Next aTab

When working with the Paragraphs collection (or a range with several paragraphs), you must modify each paragraph in the collection individually if the tab stops aren't identical in all the paragraphs.

Dim objParagraph As Paragraph 
For Each objParagraph In ActiveDocument.Content.Paragraphs
    objParagraph.TabStops(InchesToPoints(1)).Clear
Next para


Adding Tabs

Use the Add method to add a tab stop.

objTabStopsCol.Add Position:=InchesToPoints(1), _ 
                   Alignment:=wdTabAlignment.wdAlignTabLeft, _
                   Leader:=wdTabLeader.wdTabLeaderSpaces

You can also add a tab by specifying the location with the TabStops property

Selection.Paragraphs.TabStops(InchesToPoints(2)).Alignment = wdTabAlignment.wdAlignTabLeft 

This is equivalent to the following line:

Selection.Paragraphs.TabStops.Add Position:=InchesToPoints(2), _ 
                                  Alignment:=wdTabAlignment.wdAlignTabLeft

This adds a tab stop positioned at 2.5 inches to the selected paragraphs and then displays the position of each item in the TabStops collection.

Selection.Paragraphs.TabStops.Add Position:=InchesToPoints(2.5) 

This adds a tab stop that is a left-aligned tab with a dotted tab leader positioned at 1 inch (72 points).

Selection.Paragraphs.TabStops.Add Position:=InchesToPoints(1), _ 
                                  Leader:=wdTabLeader.wdTabLeaderDots, _
                                  Alignment:=wdTabAlignment.wdAlignTabLeft

This adds a tab that is centred and is positioned at 2 inches.

Selection.Paragraphs.TabStops.Add Position:=InchesToPoints(2), _ 
                                  Alignment:=wdTabAlignment.wdAlignTabCenter

This adds a tab for every paragraph in the active document

ActiveDocument.Content.ParagraphFormat.TabStops.Add Position:=InchesToPoints(1), _ 
                                                    Alignment:=wdTabAlignment.wdAlignTabLeft


Removing Tabs

This removes the first custom tab stop from the first paragraph in the active document.

ActiveDocument.Paragraphs(1).TabStops(1).Clear 


Removing all Tab Stops

This removes all the tabs from the whole document

ActiveDocument.Content.ParagraphFormat.TabStops.ClearAll 

objTabStopsCol.ClearAll 


Changing the Default Tab Stop Size

To change the default tab stop you must use the DefaultTabStop property of the Document object.

ActiveDocument.DefaultTabStop = InchesToPoints(1) 
objDocument.DefaultTabStop = InchesToPoints(0.5)

To locate the nearest tab stop object (i.e. the nearest tab)

objTabStopsCol.Before Position:= 
objTabStopsCol.After Position:=


Word removes all default tab stops whose positions lie to the left of a custom tab stop

objTabStop.Alignment 
objTabStop.Clear
objTabStop.CustomTab = True | False
objTabStop.Leader
objTabStop.Previous | Next
objTabStop.Position

VBA - Line Breaks


objRange.InsertBreak(wdBreakType.wdLineBreak) 




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