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:
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
I am paragraph 2
Count property returns number of paragraph available in a range or a document:
It returns 4
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