Microsoft Word Paragraph object with example

Microsoft Word Paragraph object with example

Paragraph

Represents a block of text in a document. The Document object offers Paragraphs collection which contains all available paragraphs within document and each paragraph can be referenced by an index (a positive integer). In this article we will see various example and understand paragraph object in details.

If you notice above screenshot where I placed for lines and each line represents a paragraph. Let’s put some code to get 2nd parameter text printed on the screen:

Code example

Public Sub ParagraphExample()
    'Declare Paragraph object
    Dim oParagraph As Paragraph
    
    'Get second paragraph and print text
    Set oParagraph = ActiveDocument.Paragraphs(2)
    
    'Print text
    Debug.Print oParagraph.Range.Text
End Sub

Output

I am paragraph 2

Count Property

Count property returns number of paragraph available in a range or a document:

Code example

Debug.Print ActiveDocument.Paragraphs.Count

Output

It returns 4

Add Method

Public Sub ParagraphExample()
    
    'Move end of document
    Selection.EndKey unit:=wdStory
    
    'Declare Paragraph object
    Dim oParagraph As Paragraph
    
    'Add and Get paragraph
    Set oParagraph = Selection.Paragraphs.Add
    
	'Move end of document
    Selection.EndKey unit:=wdStory
    
    'Add text text
    Selection.Range.Text = "I am paragraph 5"
End Sub

Output

Leave a Reply

Your email address will not be published. Required fields are marked *