Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Exclude Paragraph Marks Whe...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Exclude Paragraph Marks When Creating Ranges

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

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.

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.

    Visual Basic
    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
    
    
    C#
    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 add-in. This code uses the active document.

    Visual Basic
    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
    
    
    C#
    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.

    Visual Basic
    firstRange.Text = secondString
    secondRange.Text = firstString
    
    
    C#
    firstRange.Text = secondString; 
    secondRange.Text = firstString; 
    
    
  3. Select each range in turn and pause to display the results in a message box.

    Visual Basic
    firstRange.Select()
    MessageBox.Show(firstRange.Text)
    secondRange.Select()
    MessageBox.Show(secondRange.Text)
    
    
    C#
    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.

    Visual Basic
    firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=-1)
    
    
    C#
    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.

    Visual Basic
    firstRange.Text = "New content for paragraph 1."
    
    
    C#
    firstRange.Text = "New content for paragraph 1.";
    
    
  6. Replace the text in secondRange, including the paragraph mark.

    Visual Basic
    secondRange.Text = "New content for paragraph 2."
    
    
    C#
    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.

    Visual Basic
    firstRange.Select()
    MessageBox.Show(firstRange.Text)
    secondRange.Select()
    MessageBox.Show(secondRange.Text)
    
    
    C#
    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.

    Visual Basic
    firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=1)
    
    
    C#
    move = 1;  // move right 1
    firstRange.MoveEnd(ref charUnit, ref move); 
    
    
  9. Delete secondRange. This restores paragraph three to its original position.

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

    Visual Basic
    firstRange.Text = firstString
    
    
    C#
    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.

    Visual Basic
    firstRange.InsertAfter(secondString)
    firstRange.Select()
    
    
    C#
    firstRange.InsertAfter(secondString); 
    firstRange.Select(); 
    
    

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.

    Visual Basic
    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
    
    
    C#
    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(); 
    }
    
    

To control paragraph structure when inserting text in an application-level add-in

  • The following example shows the complete method for an application-level add-in. To use this code, run it from the ThisAddIn class in your project.

    Visual Basic
    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
    
    
    C#
    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();
    }
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker