none
How to write multiple paragraph in one line in MS word ,in c# RRS feed

  • Question

  • How write multiple paragraph in one line  in MS word in c#.

    Paragraph.Range.text = "Testamatic".

    Paragraph.InsertParagrahAfter() ;

    Paragraph1.Range.text = "Systems".

    Paragraph1.InsertParagrahAfter() ;

    Paragraph2.Range.text = "Systems".

    Paragraph2.InsertParagrahAfter() ;

    Paragraph3.Range.text = "Bangalore".

    Paragraph3.InsertParagrahAfter() ;


    O/p  in MS word

    Testamatic Systems  Systems Bangalore


    Mukesh Kumar



    Friday, December 13, 2013 4:30 AM

Answers

  • Hello Mukesh,

    Why do you need to use paragraphs?

    Instead, you can use the Font class to get the job done, for example:

    Word.Document doc = WordApp.ActiveDocument;
    Word.Paragraphs pars = doc.Paragraphs;
    Word.Paragraph par = pars.Add();
    Word.Range rng = par.Range;            
    
    for(int  i = 0; i<20 ;i++)
    {                
       rng.InsertAfter(i.ToString() +",");
    
       Word.Words words = rng.Words;
       Word.Range range = words[words.Count];
    
       // you can also use the Characters collection 
       // Word.Characters chars = rng.Characters;                
       // Word.Range range = chars[chars.Count - 1];                
    
       Word.Font font = range.Font;
       if (i % 2 == 0)
       {                    
          font.Size = 20;
          font.Bold = 4;
          font.Underline = Word.WdUnderline.wdUnderlineSingle;
          font.ColorIndex = Word.WdColorIndex.wdBlue;
       }
       else
       {                 
          font.Size = 10;
          font.ColorIndex = Word.WdColorIndex.wdGray25;
       }                
    }
    • Marked as answer by mukeshkumarts Tuesday, December 17, 2013 4:17 AM
    Monday, December 16, 2013 3:28 PM
  • The properties you list are not PARAGRAPH properties, they're character properties. You do NOT want to insert multiple paragraphs for things that should appear in a single line. Please just forget that VERY BAD and VERY INCORRECT approach.

    Before you can do anything like this you have to explain how the code inside the loop should know when to apply which formatting. Without that informtion it's impossible to help you.

    Again, I urge you to define STYLES for the different formatting combinations rather than applying them one after the other.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 16, 2013 4:30 PM
    Moderator

All replies

  • Hi mukeshkumarts

    I seem to recall seeing this same question a few days ago, but now I can't find that discussion... was that you, or someone else?

    Why would you want multiple lines in the same paragraph? That doesn't really make a lot of sense - I know of only one use case where that is done, and it bears no resemblance to your example.

    Why wouldn't you do the following:

    Word.Range rng = Paragraph.Range;
    rng.Text = "Testamatic Systems Systems Bangalore";
    rng.Font.Underline = Word.WdUnderline.wdUnderlineWords;


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, December 13, 2013 9:57 AM
    Moderator
  • How write multiple paragraph in one line  in MS word in c#.

     

    for(int  i = 0; i<20 ;i++)

    {

    Paragraph.Range.text = i.tostring() +",".

    Paragraph.InsertParagrahAfter() ;

    }

    O/p 

    in word

    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,


    Mukesh Kumar

    Friday, December 13, 2013 11:59 AM
  • Thanks for reply Cindy .

    Hi  Cindy Meister,

    i want  to do  in for loop  because  i have  string array.  i am try to add  by each paragraph.

    In for loop ,

    str[0] = " Testmatic "

    str[1] = " Systems"

    str[2] = " Systems"

    str[3] = " bangalore"

    .

    .

    .

    for( i = 0 ; i < 10 ; i++)

    {

    Paragraph.Range.text = str[i];

     para.Range.Font.Size = 12 ;

    para.Range.Font.Color= Black ;

    Paragraph.InsertParagrahAfter() ;

    }

    O/p  in MS word

    Testamatic Systems  Systems Bangalore .........



    Mukesh Kumar

    Friday, December 13, 2013 12:10 PM
  • In that case:

    Word.Range rng = Paragraph1.Range;
    string strText;

    for( i = 0 ; i < 10 ; i++)
    {
      strText+= str[i];
    }
    rng.text = strText;
    rng.Font.Size = 12 ;
    rng.Font.Color= Black ;
    rmg.Paragraphs[1].InsertParagrahAfter() ;

    The reason you should not populate the Range inside the loop is that each call to the Word application will slow down execution of your code. Create the entire string in the loop, then assign that to Word in one step.

    A similar remark for the formatting: If you'll be using this formatting more than one, create a paragraph style with this definition then assign the style - again, one step. Plus, if someone should decide to use a different font size (for example) you need to change it only at one point - and you know where to change it.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by mukeshkumarts Saturday, December 14, 2013 5:05 AM
    • Unmarked as answer by mukeshkumarts Monday, December 16, 2013 10:15 AM
    Friday, December 13, 2013 3:18 PM
    Moderator
  • Thanks for reply Cindy .

    Hi  Cindy Meister,

    i want  to do  in for loop  because  i have  string array.  i am try to add  by each paragraph with different paragraph properties.

    How write multiple paragraph  with different properties (Color, fontSize .......)in one line  in MS word in c#.

     

    for(int  i = 0; i<20 ;i++)

    {

    Paragraph.Range.text = i.tostring() +",".

    Paragraph.Range.FontSize = i * 3 ;

    Paragraph.Range.Color = Red , Black .......

    Paragraph.InsertParagrahAfter() ;

    }

    O/p 

    in word

    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,


    Mukesh Kumar

    Monday, December 16, 2013 10:12 AM
  • How write multiple paragraph  with different properties (Color, fontSize .......)in one line  in MS word in c#.

     

    for(int  i = 0; i<20 ;i++)

    {

    Paragraph.Range.text = i.tostring() +",".

    Paragraph.Range.FontSize = i * 3 ;

    Paragraph.Range.Color = Red , Black .......

    Paragraph.InsertParagrahAfter() ;

    }

    O/p 

    in word

    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,



    Mukesh Kumar


    Monday, December 16, 2013 10:15 AM
  • Hello Mukesh,

    Why do you need to use paragraphs?

    Instead, you can use the Font class to get the job done, for example:

    Word.Document doc = WordApp.ActiveDocument;
    Word.Paragraphs pars = doc.Paragraphs;
    Word.Paragraph par = pars.Add();
    Word.Range rng = par.Range;            
    
    for(int  i = 0; i<20 ;i++)
    {                
       rng.InsertAfter(i.ToString() +",");
    
       Word.Words words = rng.Words;
       Word.Range range = words[words.Count];
    
       // you can also use the Characters collection 
       // Word.Characters chars = rng.Characters;                
       // Word.Range range = chars[chars.Count - 1];                
    
       Word.Font font = range.Font;
       if (i % 2 == 0)
       {                    
          font.Size = 20;
          font.Bold = 4;
          font.Underline = Word.WdUnderline.wdUnderlineSingle;
          font.ColorIndex = Word.WdColorIndex.wdBlue;
       }
       else
       {                 
          font.Size = 10;
          font.ColorIndex = Word.WdColorIndex.wdGray25;
       }                
    }
    • Marked as answer by mukeshkumarts Tuesday, December 17, 2013 4:17 AM
    Monday, December 16, 2013 3:28 PM
  • The properties you list are not PARAGRAPH properties, they're character properties. You do NOT want to insert multiple paragraphs for things that should appear in a single line. Please just forget that VERY BAD and VERY INCORRECT approach.

    Before you can do anything like this you have to explain how the code inside the loop should know when to apply which formatting. Without that informtion it's impossible to help you.

    Again, I urge you to define STYLES for the different formatting combinations rather than applying them one after the other.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 16, 2013 4:30 PM
    Moderator