【问题标题】:C# Microsoft.Office.Interop.WordC# Microsoft.Office.Interop.Word
【发布时间】:2012-03-23 13:40:18
【问题描述】:

我将 Microsoft.Office.Interop.Word 与 C# 结合使用。我知道您可以使用 Range.Font.Bold=1 将文本设置为粗体。我的问题是我的句子很长,我必须将其中的一些单词加粗,而不是整个句子。如果我的句子是“您希望通过电子邮件向您发送对您问题的回复吗?”,我希望“有回复”加粗。

在这个例子中,我只能加粗一个单词(通过循环遍历整个 word 文档):

foreach(Microsoft.Office.Interop.Word.Range w in oDoc.Words)
{
    if (w.Text == "Something")
         w.Font.Bold = 1;
}

但这只是一个单词,我怎么能在一个句子中加粗两个、三个或多个连续的单词。

【问题讨论】:

    标签: c# ms-word interop bold


    【解决方案1】:

    无需遍历整个文档。使用 Word.WdReplace.wdReplaceAll,类似这样:

    private void SearchReplace()
    {
        Word.Find findObject = Application.Selection.Find;
        findObject.ClearFormatting();
        findObject.Text = "find me";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = "Found";
    
        object replaceAll = Word.WdReplace.wdReplaceAll;
        findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref replaceAll, ref missing, ref missing, ref missing, ref missing);
    }
    

    您可以在此处阅读更多信息:http://msdn.microsoft.com/en-us/library/f65x8z3d.aspx

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      看看这个:

      C#: Searching a Text in Word an getting the range of the result

      然后你可以将找到的范围加粗。

      【讨论】:

        猜你喜欢
        • 2022-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多