Click here to Skip to main content
15,887,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't find a solution on how change the color of a part of a line.
It should have to do with a selection of a rang.text.
Like para.Range.Text = "Chrysanthemum Curve";
And making the part 'Curve' blue.

C#
// Modify to suit your machine:
// Get the Word application object.
Word._Application word_app = new Word.Application();

// Make Word visible (optional).
word_app.Visible = true;
object oEndOfDoc = "\\endofdoc";
// Create the Word document.
object missing = Type.Missing;
Word._Document word_doc = word_app.Documents.Add(
    ref missing, ref missing, ref missing, ref missing);

// Create a header paragraph.
Word.Paragraph para = word_doc.Paragraphs.Add(ref missing);
para.Range.Text = "Chrysanthemum Curve";
//para.Range.Text = "Curve";
object style_name = "Heading 1";
para.Range.set_Style(ref style_name);
para.Range.InsertParagraphAfter();

// Add more text. Curve"
para.Range.Text = "To make a chrysanthemum curve, " +
    "use the following parametric equations as t goes " +
    "from 0 to 21 * ? to generate " +
    "points and then connect them.";
para.Range.InsertParagraphAfter();
string qa = "";
qa = para.Range.Text.Substring(1, 2);
// Save the current font and start using Courier New.
string old_font = para.Range.Font.Name;
para.Range.Font.Name = "Courier New";
para.Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdByAuthor;

// Add the equations.
para.Range.Text =
    "  r = 5 * (1 + Sin(11 * t / 5)) -\n" +
    "      4 * Sin(17 * t / 3) ^ 4 *\n" +
    "      Sin(2 * Cos(3 * t) - 28 * t) ^ 8\n" +
    "  x = r * Cos(t)\n" +
    "  y = r * Sin(t)";

// Start a new paragraph and then
// switch back to the original font.
para.Range.InsertParagraphAfter();
para.Range.Font.Name = old_font;
Posted
Updated 21-May-15 21:10pm
Comments
ZurdoDev 21-May-15 11:50am    
Just record a macro doing it manually and then you can see what the code will be.
Member 11270681 22-May-15 3:13am    
That doesn't work.

Sub Macro1()
'
' Macro1 Macro
'
'
Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:=wdExtend
End Sub

ZurdoDev 22-May-15 7:40am    
That is weird. You're right it does not change the color.

But if you use Intellisense you can see that there is a Selection.Font.ColorIndex. Try that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900