Compartilhar via


How to: Programmatically exclude paragraph marks when creating ranges

Applies to: yesVisual Studio noVisual 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

  1. 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;
    
  2. Assign the Text property, swapping the text between the two paragraphs.

    firstRange.Text = secondString
    secondRange.Text = firstString
    
    firstRange.Text = secondString; 
    secondRange.Text = firstString;
    
  3. 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);
    
  4. Adjust firstRange using the MoveEnd method so that the paragraph marker is no longer a part of firstRange.

    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);
    
  5. 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.";
    
  6. Replace the text in secondRange, including the paragraph mark.

    secondRange.Text = "New content for paragraph 2."
    
    secondRange.Text = "New content for paragraph 2.";
    
  7. Select firstRange and pause to display the results in a message box, and then do the same with secondRange.

    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 in secondRange, 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.

  8. 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);
    
  9. Delete secondRange. This restores paragraph three to its original position.

    secondRange.Delete()
    
    secondRange.Delete(ref missing, ref missing);
    
  10. Restore the original paragraph text in firstRange.

    firstRange.Text = firstString
    
    firstRange.Text = firstString;
    
  11. Use the InsertAfter method of the Range object to insert the original paragraph-two content after firstRange, and then select firstRange.

    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

  1. 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

  1. 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