Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...