none
how to select some Characters in paragraph RRS feed

  • Question

  • i want to change some text in a paragraph,  the code bellow can't be work,

    Paragraph pf = this.piaDocument.Paragraphs[this.piaDocument.Paragraphs.Count];
    pf.Range.InsertParagraphAfter();
    Paragraph p = this.piaDocument.Paragraphs.Add(pf.Range);
    p.Range.Text = "some text with diff style";
    object style = WdBuiltinStyle.wdStyleStrong;
    p.Range.SetRange(1, 5);
    p.Range.Select();
    p.Range.set_Style(ref style);

    i imaging the output text will be "some text with diff style", but all of them with strong style ("some text with diff style"). 

    any one can help me,,tks!

    Wednesday, November 27, 2013 12:58 PM

Answers

  • Hello sendreams,

    That is because each time you get a new Range object. You need to break calls to properties and methods to separate lines:

    Word.Paragraph pf = WordApp.ActiveDocument.Paragraphs[WordApp.ActiveDocument.Paragraphs.Count];
    pf.Range.InsertParagraphAfter();
    Word.Paragraph p = WordApp.ActiveDocument.Paragraphs.Add(pf.Range);
    p.Range.Text = "some text with diff style";
    object style = Word.WdBuiltinStyle.wdStyleStrong;            
     
    // always keep in mind to break the code for separate lines
    Word.Range range = p.Range;
    range.SetRange(1, 5);
    range.Select();
    range.set_Style(ref style);
    Marshal.ReleaseComObject(range);
    range = null;

    This code works like a charm in my Word 2013.
    • Edited by Eugene Astafiev Wednesday, November 27, 2013 2:34 PM
    • Marked as answer by sendreams Thursday, November 28, 2013 7:24 AM
    Wednesday, November 27, 2013 2:33 PM

All replies


  • the post before had lost html style. i really mean is:

    i imaging the output text will be "some text with diff style", but all of them with strong style ("some text with diff style"). 

    Wednesday, November 27, 2013 2:21 PM
  • my god,, this forum has bug for text style setting.

    i mean the real result is all of the output is with strong style, but in my code i want to set  characters "some" to strong only.

    Wednesday, November 27, 2013 2:27 PM
  • Hello sendreams,

    That is because each time you get a new Range object. You need to break calls to properties and methods to separate lines:

    Word.Paragraph pf = WordApp.ActiveDocument.Paragraphs[WordApp.ActiveDocument.Paragraphs.Count];
    pf.Range.InsertParagraphAfter();
    Word.Paragraph p = WordApp.ActiveDocument.Paragraphs.Add(pf.Range);
    p.Range.Text = "some text with diff style";
    object style = Word.WdBuiltinStyle.wdStyleStrong;            
     
    // always keep in mind to break the code for separate lines
    Word.Range range = p.Range;
    range.SetRange(1, 5);
    range.Select();
    range.set_Style(ref style);
    Marshal.ReleaseComObject(range);
    range = null;

    This code works like a charm in my Word 2013.
    • Edited by Eugene Astafiev Wednesday, November 27, 2013 2:34 PM
    • Marked as answer by sendreams Thursday, November 28, 2013 7:24 AM
    Wednesday, November 27, 2013 2:33 PM
  • i got it, tks,, the skill may be powerful in another scene.
    Thursday, November 28, 2013 2:01 AM