不同的格式合并为一行 Interop.word

发布于 2024-10-31 10:05:25 字数 118 浏览 3 评论 0原文

我一直在尝试弄清楚如何使用 c# 中的 interop.word 将 2 种不同的格式插入到同一个段落中,如下所示:

hello Planet earth 这是我想要做的

I've been trying to figure out how to insert 2 different formats into the same paragraph using interop.word in c# like this:

hello planet earth here's what I want to do

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

暮年 2024-11-07 10:05:25

假设您将文档定义为 oDoc,以下代码应该会获得所需的结果:

Word.Paragraph oPara = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara.Range.Text = "hello planet earth here's what I want to do";
object oStart = oPara.Range.Start + 13;
object oEnd = oPara.Range.Start + 18;

Word.Range rBold = oDoc.Range(ref oStart, ref oEnd);
rBold.Bold = 1;

Assuming you have your document defined as oDoc, the following code should get you the desired result:

Word.Paragraph oPara = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara.Range.Text = "hello planet earth here's what I want to do";
object oStart = oPara.Range.Start + 13;
object oEnd = oPara.Range.Start + 18;

Word.Range rBold = oDoc.Range(ref oStart, ref oEnd);
rBold.Bold = 1;
风筝有风,海豚有海 2024-11-07 10:05:25

我必须稍微修改丹尼斯的答案才能使其对我有用。

我正在做的事情是完全自动化的,所以我只需要使用变量。

private void InsertMultiFormatParagraph(string text, int size, int spaceAfter = 10) {
    var para = docWord.Content.Paragraphs.Add(ref objMissing);

    para.Range.Text        = text;
    // Explicitly set this to "not bold"
    para.Range.Font.Bold   = 0;
    para.Range.Font.Size   = size;
    para.Format.SpaceAfter = spaceAfter;

    var start = para.Range.Start;
    var end   = para.Range.Start + text.IndexOf(":");

    var rngBold = docWord.Range(ref objStart, ref objEnd);
    rngBold.Bold = 1;

    para.Range.InsertParagraphAfter();
}

让我想发表这篇文章的主要区别是应该在更改字体后插入段落。我最初的想法是在设置 SpaceAfter 属性后插入它,但随后 objStart 和 objEnd 值抛出“OutOfRange”异常。这有点违反直觉,所以我想确保每个人都知道。

I had to modify Dennis' answer a little to get it to work for me.

What I'm doing it totally automated, so I have to only work with variables.

private void InsertMultiFormatParagraph(string text, int size, int spaceAfter = 10) {
    var para = docWord.Content.Paragraphs.Add(ref objMissing);

    para.Range.Text        = text;
    // Explicitly set this to "not bold"
    para.Range.Font.Bold   = 0;
    para.Range.Font.Size   = size;
    para.Format.SpaceAfter = spaceAfter;

    var start = para.Range.Start;
    var end   = para.Range.Start + text.IndexOf(":");

    var rngBold = docWord.Range(ref objStart, ref objEnd);
    rngBold.Bold = 1;

    para.Range.InsertParagraphAfter();
}

The main difference that made me want to make this post was that the Paragraph should be inserted AFTER the font is changed. My initial thought was to insert it after setting the SpaceAfter property, but then the objStart and objEnd values were tossing "OutOfRange" Exceptions. It was a little counter-intuitive, so I wanted to make sure everyone knew.

坏尐絯 2024-11-07 10:05:25

在格式化段落中的特定选择时,以下代码似乎最适合我。使用Word 内置的“查找”功能进行选择,然后仅设置所选文本的格式。仅当要选择的文本是所选内容中的唯一字符串时,此方法才有效。但对于我遇到的大多数情况来说,这似乎是有效的。

        oWord.Selection.Find.Text = Variable_Containing_Text_to_Select; // sets the variable for find and select
        oWord.Selection.Find.Execute(); // Executes find and select
        oWord.Selection.Font.Bold = 1; // Modifies selection
        oWord.Selection.Collapse();  // Clears selection

希望这对某人有帮助!

The following code seemed to work the best for me when formatting a particular selection within a paragraph. Using Word's built in "find" function to make a selection, then formatting only the selected text. This approach would only work well if the text to select is a unique string within the selection. But for most situations I have run across, this seems to work.

        oWord.Selection.Find.Text = Variable_Containing_Text_to_Select; // sets the variable for find and select
        oWord.Selection.Find.Execute(); // Executes find and select
        oWord.Selection.Font.Bold = 1; // Modifies selection
        oWord.Selection.Collapse();  // Clears selection

Hope this helps someone!

末骤雨初歇 2024-11-07 10:05:25

我知道这篇文章很旧,但它几乎出现在我所有的搜索中。下面的答案是为了防止像我这样的人想要对句子中的多个单词执行此操作。在本例中,我循环遍历包含字符串的变量字符串数组,并将该文本更改为粗体 -modifing @joshman1019

string[] makeBold = new string[4] {a, b, c, d};

foreach (string s in makeBold)
{
   wApp.Selection.Find.Text = s; //changes with each iteration
   wApp.Selection.Find.Execute(); 
   wApp.Selection.Font.Bold = 1;
   wApp.Selection.Collapse(); //used to 'clear' the selection
   wApp.Selection.Find.ClearFormatting();
}

因此,该变量表示的每个字符串都将是粗体。因此,如果 a = "hello world",则 Hello World 在 Word 文档中会变为粗体。希望它可以节省一些时间。

I know this post is old, but it came out in almost all my searches. The answer below is in case someone, like me, wants to do this for more than one word in a sentence. In this case, I loop through a string array of variables that contain strings and change that text to bold--modifing @joshman1019

string[] makeBold = new string[4] {a, b, c, d};

foreach (string s in makeBold)
{
   wApp.Selection.Find.Text = s; //changes with each iteration
   wApp.Selection.Find.Execute(); 
   wApp.Selection.Font.Bold = 1;
   wApp.Selection.Collapse(); //used to 'clear' the selection
   wApp.Selection.Find.ClearFormatting();
}

So, each string represented by the variable will be bold. So if a = "hello world", then Hello World is made bold in the Word doc. Hope it saves someone some time.

三生一梦 2024-11-07 10:05:25

最终考虑使用 Range.Collapse 并将 Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd 作为参数。
这将允许下一个文本的格式与前一个文本不同(并且下一个文本格式不会影响前一个文本的格式)。

Consider usage of Range.Collapse eventually with Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd as parameter.
That would allow next text to have formatting different than previous text (and next text formatting will not affect formatting of previous one).

旧时浪漫 2024-11-07 10:05:25

我知道这是一个旧线程,但我想无论如何我都会在这里发布给那些通过谷歌遇到它的人(就像我所做的那样)。我用 krillgar 的方法找到了大部分解决方案,但我遇到了麻烦,因为我的一些文本包含换行符。因此,这种修改对我来说效果最好:

private void WriteText(string text)
    {
        var para = doc.Content.Paragraphs.Add();
        var start = para.Range.Start;
        var end = para.Range.Start + text.IndexOf(":");
        para.Range.Text = text;
        para.Range.Font.Bold = 0;
        para.Range.InsertParagraphAfter();

        if(text.Contains(":")){
            var rngBold = doc.Range(start, end);
            rngBold.Bold = 1;
        }
    }

关键的区别是我在函数中更早地计算开始和结束。我不太清楚,但我认为如果您的新文本中有换行符,则稍后开始/结束的计算会弄乱一些东西。

显然,我的解决方案适用于格式如下的文本:

标签:数据

,其中标签要加粗。

I know this is an old thread, but I thought I'd post here anyway for those that come across it via Google (like I did). I got most of the way to a solution with krillgar's approach, but I had trouble because some of my text contains newlines. Accordingly, this modification worked best for me:

private void WriteText(string text)
    {
        var para = doc.Content.Paragraphs.Add();
        var start = para.Range.Start;
        var end = para.Range.Start + text.IndexOf(":");
        para.Range.Text = text;
        para.Range.Font.Bold = 0;
        para.Range.InsertParagraphAfter();

        if(text.Contains(":")){
            var rngBold = doc.Range(start, end);
            rngBold.Bold = 1;
        }
    }

The key difference is that I calculate start and end earlier in the function. I can't quite put my finger on it, but I think if your new text has newlines in it, the later calculation of start/end messes something up.

And obviously my solution is intended for text with the format:

Label: Data

where Label is to be bolded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文