none
how to set the bold the text in the document programmatically RRS feed

  • Question

  • Hi,

    I have a code here that insert a text in the document

    Document wordDocument = null;

    wordDocument.Content.InsertAfter("Reference");

    and I want that text to bold ,change font style and put it in a center of the document just like this function is ms word(ctrl+e).

    thank you.

    Thursday, February 6, 2014 3:27 AM

Answers

  • The appropriate way to do this is to either:
    • modify the relevant Paragraph Style;
    • create a new Paragraph Style with the required attributes; or
    • apply an appropriate Character Style.

    For example, using VBA:

    Sub Demo()
    Dim Rng As Range
    Set Rng = ActiveDocument.Range.Characters.Last
    With Rng
      .InsertAfter "Reference"
      .MoveEnd wdCharacter, -1
      .Style = "Strong"
    End With
    Set Rng = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, February 6, 2014 3:47 AM
  • Hello,


        This method may help you.

        
    private void InsertMultiFormatParagraph(string psText, int piSize, int piSpaceAfter = 10) {
        Word.Paragraph para = mdocWord.Content.Paragraphs.Add(ref mobjMissing);
    
        para.Range.Text = psText;
        // Explicitly set this to "not bold"
        para.Range.Font.Bold = 0;
        para.Range.Font.Size = piSize;
        para.Format.SpaceAfter = piSpaceAfter;
    
        object objStart = para.Range.Start;
        object objEnd = para.Range.Start + psText.IndexOf(":");
    
        Word.Range rngBold = mdocWord.Range(ref objStart, ref objEnd);
        rngBold.Bold = 1;
    
        para.Range.InsertParagraphAfter();
    }
    -----------------------------------------
     if the reply help you mark it as your answer.
     Free Managed .NET Word Component(Create, Modify, Convert & Print)
    Thursday, February 6, 2014 4:12 AM
  • << i have no idea about vb :(>>

    It's not that hard, really...

    The typing of the variable follows the variable name in VB, instead of preceding it. Instead of Dim rng as Range you would use:

    Word.Range rng;

    Where you see - With rng - remove the "With" and place "rng" before the . in the lines that follow. So: rng.InsertAfter; rng.MoveEnd; etc.

    VB doesn't require the method parentheses - (); C# requires them. As soon as you type an opening ( after a method name you should get Intellisense for the parameters. This will give you the information for fully qualifying the enumerations, such as wdCharacter.

    The equivalent of Set Rng = Nothing is: rng = null;


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, February 7, 2014 8:55 AM
    Moderator

All replies

  • The appropriate way to do this is to either:
    • modify the relevant Paragraph Style;
    • create a new Paragraph Style with the required attributes; or
    • apply an appropriate Character Style.

    For example, using VBA:

    Sub Demo()
    Dim Rng As Range
    Set Rng = ActiveDocument.Range.Characters.Last
    With Rng
      .InsertAfter "Reference"
      .MoveEnd wdCharacter, -1
      .Style = "Strong"
    End With
    Set Rng = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, February 6, 2014 3:47 AM
  • Hello,


        This method may help you.

        
    private void InsertMultiFormatParagraph(string psText, int piSize, int piSpaceAfter = 10) {
        Word.Paragraph para = mdocWord.Content.Paragraphs.Add(ref mobjMissing);
    
        para.Range.Text = psText;
        // Explicitly set this to "not bold"
        para.Range.Font.Bold = 0;
        para.Range.Font.Size = piSize;
        para.Format.SpaceAfter = piSpaceAfter;
    
        object objStart = para.Range.Start;
        object objEnd = para.Range.Start + psText.IndexOf(":");
    
        Word.Range rngBold = mdocWord.Range(ref objStart, ref objEnd);
        rngBold.Bold = 1;
    
        para.Range.InsertParagraphAfter();
    }
    -----------------------------------------
     if the reply help you mark it as your answer.
     Free Managed .NET Word Component(Create, Modify, Convert & Print)
    Thursday, February 6, 2014 4:12 AM
  • Hi Sir macropod,

    sir do you have a c# code for that sir? cause i have no idea about vb :(

    Friday, February 7, 2014 4:25 AM
  • Sorry, but I don't know c#...

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, February 7, 2014 4:47 AM
  • << i have no idea about vb :(>>

    It's not that hard, really...

    The typing of the variable follows the variable name in VB, instead of preceding it. Instead of Dim rng as Range you would use:

    Word.Range rng;

    Where you see - With rng - remove the "With" and place "rng" before the . in the lines that follow. So: rng.InsertAfter; rng.MoveEnd; etc.

    VB doesn't require the method parentheses - (); C# requires them. As soon as you type an opening ( after a method name you should get Intellisense for the parameters. This will give you the information for fully qualifying the enumerations, such as wdCharacter.

    The equivalent of Set Rng = Nothing is: rng = null;


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, February 7, 2014 8:55 AM
    Moderator
  • Hi Sir macropod,

    okay sir Thanks anyway.

    more power to you god bless.

    Friday, February 7, 2014 10:30 AM