none
How to set paragraph number in word RRS feed

  • Question

  • Hi,

    I have word document like below

    1.This is first para
    2.This is second para
     2.1 This 2.1 paragraph
     2.2 This 2.2 paragraph
    3.This is third paragraph

    I want to insert the text after the cursor placed current paragraph and numbers should change automatically.

    I have written the code like this. It is working for all the condition except when i placed the cursor in 2.2 paragraph
     
    new text is adding after 2.2 paragraph with paragraph number 3 instead of 2.3.

    Please see my code and help me where i am doing mistake.

        object start = 0;
        object end = Globals.ThisAddIn.Application.Selection.Paragraphs[1].Range.End;
        int paraIndex = Globals.ThisAddIn.Application.ActiveDocument.Range(ref start2, ref end2).Paragraphs.Count;
        Word.Range wrPara = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[paraIndex2].Range;
        wrPara.InsertAfter("Inserted Paragraph");

    Please help me on this.

    Thanks

    Nagaravikumar


    ravi

    Wednesday, August 22, 2012 2:50 PM

Answers

  • Wednesday, August 22, 2012 5:59 PM
    Moderator
  • Hi Ravi

    And did you try changing the level using the property I suggest in that answer? If you change the level, it should "move in" from Level 1 to Level 2.

    It's impossible to tell from the code snippet you show us what should be happening. There's no information about what the variables hold that you pass to the method parameters in your code. So there's no way I can tell you how to change your existing code to "make it work".

    And how your approach would work depends on various other factors, as well. For example, I can't know if the numbering levels in your document are linked to styles, or not. If they are, then all you'd need to do would be to apply the style to the paragraph.

    It's also not possible for us to tell whether you're inserting a paragraph after 2.2, as you think. And if you are, whether the style associated with 2.2 has as it's "following style" the style associated with Level 1. All of these things will affect what happens when paragraphs are added to a list.

    So the only recommendation I can make that I can be fairly sure should work is to tell you to change the numbering level of the paragraph.


    Cindy Meister, VSTO/Word MVP

    Thursday, August 23, 2012 4:34 PM
    Moderator

All replies

  • Wednesday, August 22, 2012 5:59 PM
    Moderator
  • Hi Cindy,

    Thank you for your reply... The link you have provided is for sub paragraph numbering.

    Here the issue is when i place the cursor in paragraph 1 and executed the above code, it is inserted after first paragraph and numbered as 2 and subsequent numbers changed to 3 and 3.1..and so. Perfect... But when i place the cursor in 2.1 paragraph working fine. Only when i place the cursor in 2.2 paragraph (last sub paragraph in 2nd) then the  inserted paragraph number is giving as 3 instead of 2.3..

    1.This is first para

    2.This is second para
     2.1 This 2.1 paragraph
     2.2 This 2.2 paragraph
    3.This is third paragraph

    Thanks

    Nagaravikumr


    ravi

    Thursday, August 23, 2012 4:30 AM
  • Hi Ravi

    And did you try changing the level using the property I suggest in that answer? If you change the level, it should "move in" from Level 1 to Level 2.

    It's impossible to tell from the code snippet you show us what should be happening. There's no information about what the variables hold that you pass to the method parameters in your code. So there's no way I can tell you how to change your existing code to "make it work".

    And how your approach would work depends on various other factors, as well. For example, I can't know if the numbering levels in your document are linked to styles, or not. If they are, then all you'd need to do would be to apply the style to the paragraph.

    It's also not possible for us to tell whether you're inserting a paragraph after 2.2, as you think. And if you are, whether the style associated with 2.2 has as it's "following style" the style associated with Level 1. All of these things will affect what happens when paragraphs are added to a list.

    So the only recommendation I can make that I can be fairly sure should work is to tell you to change the numbering level of the paragraph.


    Cindy Meister, VSTO/Word MVP

    Thursday, August 23, 2012 4:34 PM
    Moderator
  • Hi Cindy..

    Thank you very much for your replay..

    It is working for me. I have completed my Task. I really thankful to you.

    Thanks

    Nagaravikumar


    ravi

    Friday, August 24, 2012 7:01 AM