How to: Programmatically exclude paragraph marks when creating ranges
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
Whenever you create a Range object based on a paragraph, all non-printing characters, such as paragraph marks, are included in the range. You may want to insert the text from a source paragraph into a destination paragraph. If you do not want to split the destination paragraph into separate paragraphs, then you must first remove the paragraph mark from the source paragraph. Additionally, since paragraph formatting information is stored within the paragraph mark, you might not want to include this when you insert the range into an existing paragraph.
Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.
The following example procedure declares two string variables, retrieves the contents of the first and second paragraphs in the active document, and then exchanges their contents. The example then demonstrates removing the paragraph marker from the range by using the MoveEnd method and inserting text inside the paragraph.
To control paragraph structure when inserting text
Create two range variables for the first and second paragraphs, and retrieve their contents using the Text property.
The following code example can be used in a document-level customization.
Dim firstRange As Word.Range = Me.Paragraphs(1).Range Dim secondRange As Word.Range = Me.Paragraphs(2).Range Dim firstString As String = firstRange.Text Dim secondString As String = secondRange.Text
Word.Range firstRange = this.Paragraphs[1].Range; Word.Range secondRange = this.Paragraphs[2].Range; string firstString = firstRange.Text; string secondString = secondRange.Text;
The following code example can be used in an application-level VSTO Add-in. This code uses the active document.
Dim document As Word.Document = Me.Application.ActiveDocument Dim firstRange As Word.Range = document.Paragraphs(1).Range Dim secondRange As Word.Range = document.Paragraphs(2).Range Dim firstString As String = firstRange.Text Dim secondString As String = secondRange.Text
Word.Document document = this.Application.ActiveDocument; Word.Range firstRange = document.Paragraphs[1].Range; Word.Range secondRange = document.Paragraphs[2].Range; string firstString = firstRange.Text; string secondString = secondRange.Text;
Assign the Text property, swapping the text between the two paragraphs.
firstRange.Text = secondString secondRange.Text = firstString
firstRange.Text = secondString; secondRange.Text = firstString;
Select each range in turn and pause to display the results in a message box.
firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text)
firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text);
Adjust
firstRange
using the MoveEnd method so that the paragraph marker is no longer a part offirstRange
.firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=-1)
object charUnit = Word.WdUnits.wdCharacter; object move = -1; // move left 1 firstRange.MoveEnd(ref charUnit, ref move);
Replace the rest of the text in the first paragraph, assigning a new string to the Text property of the range.
firstRange.Text = "New content for paragraph 1."
firstRange.Text = "New content for paragraph 1.";
Replace the text in
secondRange
, including the paragraph mark.secondRange.Text = "New content for paragraph 2."
secondRange.Text = "New content for paragraph 2.";
Select
firstRange
and pause to display the results in a message box, and then do the same withsecondRange
.Since
firstRange
was redefined to exclude the paragraph mark, the original formatting of the paragraph is preserved. However, a sentence was inserted over the paragraph mark insecondRange
, removing the separate paragraph.firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text)
firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text);
The original contents of both ranges were saved as strings, so you can restore the document to its original condition.
Readjust
firstRange
to include the paragraph mark by using the MoveEnd method for one-character position.firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=1)
move = 1; // move right 1 firstRange.MoveEnd(ref charUnit, ref move);
Delete
secondRange
. This restores paragraph three to its original position.secondRange.Delete()
secondRange.Delete(ref missing, ref missing);
Restore the original paragraph text in
firstRange
.firstRange.Text = firstString
firstRange.Text = firstString;
Use the InsertAfter method of the Range object to insert the original paragraph-two content after
firstRange
, and then selectfirstRange
.firstRange.InsertAfter(secondString) firstRange.Select()
firstRange.InsertAfter(secondString); firstRange.Select();
Document-level customization example
To control paragraph structure when inserting text in document-level customizations
The following example shows the complete method for a document-level customization. To use this code, run it from the
ThisDocument
class in your project.Private Sub ReplaceParagraphText() Dim firstRange As Word.Range = Me.Paragraphs(1).Range Dim secondRange As Word.Range = Me.Paragraphs(2).Range Dim firstString As String = firstRange.Text Dim secondString As String = secondRange.Text firstRange.Text = secondString secondRange.Text = firstString firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text) firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=-1) firstRange.Text = "New content for paragraph 1." secondRange.Text = "New content for paragraph 2." firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text) firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=1) secondRange.Delete() firstRange.Text = firstString firstRange.InsertAfter(secondString) firstRange.Select() End Sub
private void ReplaceParagraphText() { Word.Range firstRange = this.Paragraphs[1].Range; Word.Range secondRange = this.Paragraphs[2].Range; string firstString = firstRange.Text; string secondString = secondRange.Text; firstRange.Text = secondString; secondRange.Text = firstString; firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text); object charUnit = Word.WdUnits.wdCharacter; object move = -1; // move left 1 firstRange.MoveEnd(ref charUnit, ref move); firstRange.Text = "New content for paragraph 1."; secondRange.Text = "New content for paragraph 2."; firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text); move = 1; // move right 1 firstRange.MoveEnd(ref charUnit, ref move); secondRange.Delete(ref missing, ref missing); firstRange.Text = firstString; firstRange.InsertAfter(secondString); firstRange.Select(); }
VSTO Add-in example
To control paragraph structure when inserting text in a VSTO Add-in
The following example shows the complete method for a VSTO Add-in. To use this code, run it from the
ThisAddIn
class in your project.Private Sub ReplaceParagraphText() Dim document As Word.Document = Me.Application.ActiveDocument Dim firstRange As Word.Range = document.Paragraphs(1).Range Dim secondRange As Word.Range = document.Paragraphs(2).Range Dim firstString As String = firstRange.Text Dim secondString As String = secondRange.Text firstRange.Text = secondString secondRange.Text = firstString firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text) firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=-1) firstRange.Text = "New content for paragraph 1." secondRange.Text = "New content for paragraph 2." firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text) firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=1) secondRange.Delete() firstRange.Text = firstString firstRange.InsertAfter(secondString) firstRange.Select() End Sub
private void ReplaceParagraphText() { Word.Document document = this.Application.ActiveDocument; Word.Range firstRange = document.Paragraphs[1].Range; Word.Range secondRange = document.Paragraphs[2].Range; string firstString = firstRange.Text; string secondString = secondRange.Text; firstRange.Text = secondString; secondRange.Text = firstString; firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text); object charUnit = Word.WdUnits.wdCharacter; object move = -1; // move left 1 firstRange.MoveEnd(ref charUnit, ref move); firstRange.Text = "New content for paragraph 1."; secondRange.Text = "New content for paragraph 2."; firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text); move = 1; // move right 1 firstRange.MoveEnd(ref charUnit, ref move); secondRange.Delete(ref missing, ref missing); firstRange.Text = firstString; firstRange.InsertAfter(secondString); firstRange.Select(); }
See also
- How to: Programmatically extend ranges in documents
- How to: Programmatically collapse ranges or selections in documents
- How to: Programmatically insert text into Word documents
- How to: Programmatically reset ranges in Word documents
- How to: Programmatically define and select ranges in documents
- Optional parameters in Office solutions