How do I write bold text to a Word document programmatically without bolding the entire document?
Here's an answer that I came up with that will allow you to have part of a string bold and regular in the same string.
What I was doing was automated, but the same applies if you know what you're doing. Keep in mind too that the Bold is only an int, there is no boolean true/false (for some reason).
As per Ricardo's excellent point, I'll post the code here as well:
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();
}
Obviously, if you are trying to abstract this even further, you can add a parameter for the char
or string
so you can change what is being used to set the bold start/stop.
One thing to note that was discussed in the comments of the other thread was that for some reason, Bold is only an int. There is no bool value for setting that. It's weird, I know.