none
how to insert a range after a range RRS feed

  • Question

  • I would like to create a range, set its text and then set the alignment of the range to Center.

    Then, insert a new range after the first one. Set its alignment to Left. The set the text of the 2nd range to a few text lines with single line ( not paragraph ) spacing between them.

    How to do that?  In my attempt, all the text is center aligned. And the text lines have paragraph spacing between them.

    thanks,

        public void WordDemo2()
        {
          var missing = Type.Missing;
    
          var app = new Word.Application();
          var doc = app.Documents.Add(
            ref missing, ref missing, ref missing, ref missing);
    
          app.Visible = true;
    
          var range = doc.Content;
          range.Text = "Invoice";
          range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
          range.InsertParagraph();
          range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
          range.InsertAfter("Company Name, LLC" + "\n");
          range.InsertAfter("Person name" + "\n");
          range.InsertAfter("Address line 1" + "\n");
          range.InsertAfter("Address line 2" + "\n");
        }

    Saturday, November 3, 2012 6:26 PM

Answers

  • Hi Steve

    The trick is to "collapse" the Range object. Something like the following code snippet. Think about it as if everything you'd formatted as centered were a selection you made with the mouse, and that this selection would continue to grow as you add things to it. Collapsing is like pressing the left or right arrow button: you get a blinking insertion point and everything from this point on becomes a new, growing selection (the range's current content).

    Actually, I'd probably do this: range.Text = "Invoice\n"; and leave out range.InsertParagraph();

       object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd
       var range = doc.Content;
       range.Text = "Invoice";
       range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
       range.InsertParagraph();
       range.Collapse(ref oCollapseEnd);
    
       range.Paragraphs[1].Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
       range.InsertAfter("Company Name, LLC" + "\n");
       range.InsertAfter("Person name" + "\n");
       range.InsertAfter("Address line 1" + "\n");
       range.InsertAfter("Address line 2" + "\n");
    

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Steve Richter Monday, November 5, 2012 12:09 AM
    Sunday, November 4, 2012 6:13 PM
    Moderator

All replies

  • Hi Steve

    The trick is to "collapse" the Range object. Something like the following code snippet. Think about it as if everything you'd formatted as centered were a selection you made with the mouse, and that this selection would continue to grow as you add things to it. Collapsing is like pressing the left or right arrow button: you get a blinking insertion point and everything from this point on becomes a new, growing selection (the range's current content).

    Actually, I'd probably do this: range.Text = "Invoice\n"; and leave out range.InsertParagraph();

       object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd
       var range = doc.Content;
       range.Text = "Invoice";
       range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
       range.InsertParagraph();
       range.Collapse(ref oCollapseEnd);
    
       range.Paragraphs[1].Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
       range.InsertAfter("Company Name, LLC" + "\n");
       range.InsertAfter("Person name" + "\n");
       range.InsertAfter("Address line 1" + "\n");
       range.InsertAfter("Address line 2" + "\n");
    

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Steve Richter Monday, November 5, 2012 12:09 AM
    Sunday, November 4, 2012 6:13 PM
    Moderator
  • Instead of collapsing the range, you should also be able to use:

    range.Paragraphs.Last.Range.ParagraphFormat.Alignment

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, November 4, 2012 9:01 PM