Understanding Paragraph Marks
When you create a Range object that represents a Character, Word, or Sentence object, and that object falls at the end of a paragraph, the paragraph mark is automatically included within the range. The Range object also includes all additional subsequent empty paragraph marks. For example, in a document where the first paragraph consists of three sentences, the following code creates a Range object that represents the last sentence in the first paragraph:
Set rngCurrentSentence = ActiveDocument.Sentences(3)
Because the rngCurrentSentence Range object refers to the last sentence in the first paragraph, that paragraph mark (and any additional empty paragraph marks) will be included in the range. If you then set the Text property of this object to a text string that didn't end with a paragraph mark, the first and second paragraphs in the document would be deleted.
When you write VBA code that manipulates text in a Word document, you must account for the presence of a paragraph mark in your text. There are two basic techniques you can use to account for paragraph marks when you are cutting and pasting text in Range objects:
Include a new paragraph mark (represented by the vbCrLf constant) in the text to be inserted in the document. This technique is illustrated in the ChangeTextSample procedure shown in "Working with Text in a Range Object".
Exclude the final paragraph mark from a Range object. The following code sample shows how to change the contents of a Range object to exclude the final paragraph mark. The example uses the Chr$() function with character code 13 to represent a paragraph mark.
Function IsLastCharParagraph(ByRef rngTextRange As Word.Range, _ Optional blnTrimParaMark As Boolean = _ False) As Boolean ' This procedure accepts a character, word, sentence, or ' paragraph Range object as the first argument and returns True ' if the last character in the range is a paragraph mark, and ' False if it is not. The procedure also accepts an optional ' Boolean argument that specifies whether the Range object ' should be changed to eliminate the paragraph mark if it ' exists. When the blnTrimParaMark argument is True, this ' procedure calls itself recursively to strip off all trailing ' paragraph marks. Dim strLastChar As String strLastChar = Right$(rngTextRange.Text, 1) If InStr(strLastChar, Chr$(13)) = 0 Then IsLastCharParagraph = False Exit Function Else IsLastCharParagraph = True If Not blnTrimParaMark = True Then Exit Function Else Do rngTextRange.SetRange rngTextRange.Start, _ rngTextRange.Start + rngTextRange.Characters.Count - 1 Call IsLastCharParagraph(rngTextRange, True) Loop While InStr(rngTextRange.Text, Chr$(13)) <> 0 End If End If End Function
In this example, the Count property of the Range object's Characters collection is used to redefine the Range object's end point.
See Also
Working with Microsoft Word Objects | The Range Object | Creating, Defining, and Redefining a Range | Working with Text in a Range Object | Determining Where the Range Is Located | Inserting Text in a Range