none
Can't add a text line below the selected table ? RRS feed

  • Question

  • Hi,

    I am wanting to add some text into the line that below the selected table, and I used below codes for this, but it is not successful.          

    aDoc.Tables[2].Select();

    Paragraph pText = WordApp.Selection.Paragraphs.Add();

    pText.Format.SpaceAfter = 10; 

    pText.Range.Text = "New Text";


    Do you know what's wrong ?

    Thanks and regards,

    Wednesday, January 9, 2013 10:34 AM

Answers

  • Hi Taibc

    There's more than one way to do it, but try something along these lines:

      Word.Table tbl = aDoc.Tables[2];
      Word.Range rng = tbl.Range;
      object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
      rng.Collapse(ref oCollapseEnd);
      rng.Text = "New Text
    \r";
      Word.Paragraph para = rng.Paragraphs[1];
      para.Format.SpaceAfter = 10;

    A table will always have a paragraph after it, no matter where it is in the document. When you collapse a table Range to its end-point, the "(virtual) cursor" will be at the beginning of that paragraph.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 9, 2013 11:47 AM
    Moderator

All replies

  • Hi Taibc

    There's more than one way to do it, but try something along these lines:

      Word.Table tbl = aDoc.Tables[2];
      Word.Range rng = tbl.Range;
      object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
      rng.Collapse(ref oCollapseEnd);
      rng.Text = "New Text
    \r";
      Word.Paragraph para = rng.Paragraphs[1];
      para.Format.SpaceAfter = 10;

    A table will always have a paragraph after it, no matter where it is in the document. When you collapse a table Range to its end-point, the "(virtual) cursor" will be at the beginning of that paragraph.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 9, 2013 11:47 AM
    Moderator
  • Hi Taibc

    There's more than one way to do it, but try something along these lines:

      Word.Table tbl = aDoc.Tables[2];
      Word.Range rng = tbl.Range;
      object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
      rng.Collapse(ref oCollapseEnd);
      rng.Text = "New Text
    \r";
      Word.Paragraph para = rng.Paragraphs[1];
      para.Format.SpaceAfter = 10;

    A table will always have a paragraph after it, no matter where it is in the document. When you collapse a table Range to its end-point, the "(virtual) cursor" will be at the beginning of that paragraph.


    Cindy Meister, VSTO/Word MVP, my blog

    Hi Cindy,

    That's great ! Thank you very much.

    I only have a small question. Why do you need to declare a new Word.Table, not using Word.Range rng = aDoc.Tables[2].Range ?

    Thursday, January 10, 2013 1:32 AM
  • I think it's kind of habbit. By writing in Cindy's way, the readability and maintainability of the code can be highly improved.

    HTH.


    Regards,
    Fermin
    What's life without whimsy?

    Thursday, January 10, 2013 5:17 AM
  • Hi Taibc

    Fermin hit the nail on the head - it's a matter of habit. I actually thought about exactly that point as I was writing the code snippet :-) I decided to leave it as there's always the danger of syntax errors when typing directly into a post, rather than in the VS IDE - and the longer the lines of code get, the more likely it is that I won't notice a typo.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, January 10, 2013 7:04 AM
    Moderator
  • Hi Taibc

    Fermin hit the nail on the head - it's a matter of habit. I actually thought about exactly that point as I was writing the code snippet :-) I decided to leave it as there's always the danger of syntax errors when typing directly into a post, rather than in the VS IDE - and the longer the lines of code get, the more likely it is that I won't notice a typo.


    Cindy Meister, VSTO/Word MVP, my blog

    Yes. :)
    Thursday, January 10, 2013 8:07 AM