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


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