none
Change text color in Word document

    Question

  • Can anyone suggest me how to change the color of text in a word document using c# or VC++?
    Monday, October 07, 2013 11:36 AM

All replies

  • You need to work with a Range object. Document.Content would be the entire main body of the document. Since you don't give any indication how you're "getting" the text in the document, it's not possible to give you any example how to get a Range object for your particular requirement...

    Document.Content.Font.ColorIndex = Word.WdColorIndex.wdBlue;


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 08, 2013 3:57 PM
    Moderator
  • Thanks for your reply.

    I am using C++ and tried the following as you suggested..

    document->Content->Font->ColorIndex = Word::WdColorIndex::WdBlack;

    It is not returning any error but the blue font is not getting applied.I am converting the word document to PDF.

    I want to change the color of the text to black ..

    Can you please through some light here..

    Thursday, October 10, 2013 5:17 AM
  • >>document->Content->Font->ColorIndex = Word::WdColorIndex::WdBlack;

    It is not returning any error but the blue font is not getting applied<<

    Well, no, the font would not turn blue since you're passing in the black enum.

    Please note that the enum would be: wdBlack and not WdBlack, in case that makes a difference in C++

    You mention converting to PDF: are you checking the document before doing the conversion, just to make sure that there's no change? (Not that converting to PDF should in some way revert to the other color, but it's important to be sure...)

    Unfortunately, I can't help you with C++ code; C# is the best I can do in C-speak. So I don't know if there are problems with C++ where you might have to first create a Range object, then a Font object, etc.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 10, 2013 12:59 PM
    Moderator
  • Hi Cindy,

    Sorry i mentioned black instead of blue...You can suggest me in C# also..

    My aim actually is to change the text color in word document to blue and convert the document to PDF.

    The color is not getting applied even for the document also..

    Could you suggest an alternative for this..

    Thanks

    Teja Reddy

    Thursday, October 17, 2013 7:48 AM
  • I'm afraid I don't have anything more I can suggest, the code I proposed before works just fine, here: all the text in the body of the document turns blue:

                    if (wdApp != null)
                    {
                        Word.Document wdDoc = null;
                        if (wdApp.Documents.Count < 1)
                        {
                            wdDoc = wdApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                        }
                        else
                        {
                            wdDoc = wdApp.ActiveDocument;
                        }
                        wdDoc.Content.Font.ColorIndex = Word.WdColorIndex.wdBlue;
                    }
    


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 17, 2013 6:46 PM
    Moderator