views:

1017

answers:

1

I have a word document with a bookmark. I use the bookmark to get a range object which I then set the text of and add to the document. Now I want to add text after that newly added text but with different formatting how do I do this? Now I use something like

object oRangeStart = (object) previousRange.End + 1;
object oRangeEnd = (object) previousRange.End + 2;
Word.Range newRange = doc.Range(ref oRangeStart, ref oRangeEnd);
newRange.Text = "Hello, world!";

and then insert it but it shows up at the wrong place.

The reason I can't use one single range is that I want to insert something like

Name

Lorem ipsum. $99.99

and I believe I need a different range object for each style I want to apply.

+1  A: 

I typically use range to swap out text when needed. Try using Application.Selection:

Word.Selection curSel = Application.Selection;
curSel.TypeText("Type Some Text");

In your case, try this:

Word.Selection curSel;
int endOfRange =  previousRange.End + 1;
curSel.SetRange(endOfRange, endOfRange);

curSel.Font.Bold = 1;
curSel.TypeText("Hello, world!");
curSel.Font.Bold = 0;
curSel.TypeParagraph();
Mike Regan
Works perfectly and seems cleaner codewise too, thanks!
olle