如何在不加粗整个文档的情况下以编程方式将粗体文本写入 Word 文档? [英] How do I write bold text to a Word document programmatically without bolding the entire document?

查看:19
本文介绍了如何在不加粗整个文档的情况下以编程方式将粗体文本写入 Word 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要以 Office .doc 格式(非 XML)生成高度简单的报告,并且文档的某些部分需要加粗.我一直在查看 定义范围 的文档,这部分是我的代码源自眼下.文档的这一部分并没有真正给我足够的信息在我的文档中一般实现这一点的详细信息.到目前为止,这是我的代码:

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;

这会生成一个 word 文档,其中只有文本 **Information 下面的标题**.这是一个简单的例子.

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

我的文档不会比这更复杂,但我希望生成基于可变数据量的 Word 文档,粗体文本和非粗体文本分散在各处.

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.

推荐答案

这里是我想出的答案,它允许您在同一字符串中使用粗体和常规字符串的一部分.

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).

根据 Ricardo 的精彩观点,我也会在这里发布代码:

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();
}

显然,如果您想进一步抽象,您可以为 charstring 添加一个参数,以便您可以更改用于设置粗体开始/停止.

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.

在另一个线程的评论中讨论的一件事是,由于某种原因,Bold 只是一个整数.没有用于设置的布尔值.这很奇怪,我知道.

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.

这篇关于如何在不加粗整个文档的情况下以编程方式将粗体文本写入 Word 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆