Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write bold text to a Word document programmatically without bolding the entire document?

Tags:

c#

ms-word

My program needs to generate highly simple reports in the Office .doc format (non-XML), and certain parts of the document need to be bold. I've been looking at the documentation for defining ranges, which is partly what my code derives from at the moment. This part of the documentation doesn't really give me enough details to implement this in general in my document. Here is my code so far:

object miss = System.Reflection.Missing.Value;
object Visible = true;
object start = 0;

Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Document report = WordApp.Documents.Add(ref miss, ref miss, ref miss, ref miss);

String header = "Bold Header: ";
Range headerRange = report.Range(ref start, ref miss);
headerRange.Text = header;
headerRange.Font.Bold = -1;

String data = "Information underneath the header";
Range dataRange = report.Range();
dataRange.Text = data;
dataRange.Font.Bold = 1;

object filename = "test.doc";

report.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
object routeDocument = true;
WordApp.Visible = true;

This produces a word document with only the text **Information underneath the header**. This is a simple example.

My document won't get much more complicated than this, but I'm hoping to generate Word documents based on variable amounts of data, with bold text and non-bold text dispersed throughout.

like image 345
Ricardo Altamirano Avatar asked Jul 19 '12 15:07

Ricardo Altamirano


People also ask

How do I make my text Bold automatically?

One common way is to press Ctrl+B when you want to "turn on" the bold character attribute, type your word (or words), and then press Ctrl+B to turn it off. Word provides another quick way to make text bold—using asterisks.

How do Format a text into a Bold form within a document?

Make text bold . Click Bold. in the Font group on the Home tab. Type the keyboard shortcut: CTRL+B.

How do I stop Word from Autolining and Bolding?

it is attributable to an autoformat as you type setting - File > Options > Proofing > Autocorrect Options > Autoformat as you type - 'format the beginning of a list item like the one before it' and 'automatic numbered lists'. Uncheck the items on that page that you don't want to take over your formatting.


Video Answer


2 Answers

You can simply use Paragraph object to customize formatting of different text blocks. Example code as below:

object DocumentEndIndex = "\\endofdoc";
object endDocument = wordDocument.Bookmarks.get_Item(ref DocumentEndIndex).Range;
Paragraph para = wordDocument.Content.Paragraphs.Add(ref endDocument);
para.Range.Text = text;
para.Range.set_Style(ref headingLevel);
// do format the text with para.Range object as you want
para.Range.InsertParagraphAfter();

Hope this helps.

like image 189
Duy Pham Avatar answered Sep 30 '22 21:09

Duy Pham


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.

like image 41
krillgar Avatar answered Oct 04 '22 21:10

krillgar