0
votes

I have word document with numbered list.

  1. Static text 1.
  2. Bookmarked item.
  3. Static text 3.

One of those items is bookmarked and I need to replace the bookmark with text that consists of several paragraphs.

When I replace the bookmarked item with text that consists of several paragraphs I get autonumerated items for each paragraph added:

bookmark1.Range.Text = "Replaced P1."+Environment.NewLine
                      +"Replaced P2."+Environment.NewLine
  1. Static text 1.
  2. Replaced P1.
  3. Replaced P2.
  4. Static text 3.

How can I skip autonumerating and add all text to single list item?

UPD: Also numbered list style have left align, and each inserted paragraph must have the same left align.

Like this:

__1. Static text 1. more text
more text
_
_2. Replaced P1.
__Replaced P2. more text
more text
__3. Static text 3.

1

1 Answers

0
votes

Based on the accepted answer in this MSDN forum post, the key here is to use the \v escape character for the Shift+Enter key-combination (inserts a vertical tab). So, basically, you can do this:

// Get bookmark
var bookmark = myDocument.Bookmarks["myBookmark"];

// Get the list item
var listItem = bookmark.Range.ListParagraphs[1];

// Change the text using "Shift+Enter" escaped using "\v"
listItem.Range.Text = "Replacement Line 1\vReplacement Line 2\r";

(Tested with success on Word 2013 and version 15 of the Word Interop API)