首页 > 解决方案 > 如何使用 C# 在 Microsoft Word 的文本框中将特定单词加粗?

问题描述

我有一个应用程序将打印一个 .docx 文件,该文件有一个带有文本的文本框形状。其中,有些词需要加粗。

例如,部分文本是“...representando a empresa M&A, o presente...”,只有“M&A”需要加粗。

但是,我不确定我该怎么做。我在 SO 以及 MSDN 等其他网站中进行了搜索,但没有一个提供解决方案。有人可以帮我吗?

编辑:我正在使用互操作来实现这一点。到目前为止我所做的代码如下:

// opens word app
wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = false;

// print dialog for settings input
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == DialogResult.OK)
{
    wordApp.ActivePrinter = pd.PrinterSettings.PrinterName;

    // opens the document
    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(filePath);

    // iterates through each shape in the file
    foreach (Microsoft.Office.Interop.Word.Shape shape in doc.Shapes)
    {
        string shapeName = shape.Name;

        // checks whether the shape is a Text Box
        if (shapeName.StartsWith("Text Box"))
        {
            string shapeText = shape.TextFrame.ContainingRange.Text;

            // checks whether the shape is the one I want to modify 
            // side note: there are more shapes in the file, so I specify it
            if (shapeText.StartsWith("Concedemos"))
            {
                // erases the text, and sets it to a string
                shape.TextFrame.ContainingRange.Text = "";
                shape.TextFrame.ContainingRange.Text = "textSample";
            }
        }
    }

    // prints the file
    wordApp.ActiveDocument.PrintOut();
}

// quits the app
wordApp.Quit();
wordApp = null; 

提前致谢!

标签: c#ms-word

解决方案


下面的代码摘录演示了如何遍历 Word 文档中的所有形状,检查它是否是以所需字符串开头的文本框(绘图元素),然后在文本框中搜索要加粗的术语并将其加粗。

  1. 可以检查Shape.Type以确定它是否是文本框。这Shape.Type是一个Office枚举(因为该Shape对象对许多 Office 应用程序都是通用的)。所以if (shapeType == Office.MsoShapeType.msoTextBox)

  2. 要挑选文本并对其进行格式化,Word 的Range.Find功能通常是最好的方法。它可以查找和替换格式以及字符串值。(提示:在着手进行需要此类操作的项目时,通常最好在 Word UI 中的对话框 (Ctrl+H) 中进行测试,以找出正确的参数组合。)

  3. 要了解所有参数的Find.Execute用途,请查阅帮助或查看 Intellisense(或两者的组合)。

对于这个例子,重要的是要注意,为了格式化目标文本,没有Replacement.Text指定。

foreach (Microsoft.Office.Interop.Word.Shape shape in doc.Shapes)
{
    Office.MsoShapeType shapeType = shape.Type;

    // checks whether the shape is a Text Box
    if (shapeType == Office.MsoShapeType.msoTextBox)
    {
        // checks whether the shape is the one I want to modify 
        // side note: there are more shapes in the file, so I specify it
        if (shapeText.StartsWith("Concedemos"))
        {
            string textToBold = "M&A";
            Word.Range rngTextBox = shape.TextFrame.TextRange;
            Word.Find rngFind = rngTextBox.Find;
            rngFind.Text = textToBold;
            rngFind.Replacement.Font.Bold = -1;
            object oFindStop = Word.WdFindWrap.wdFindStop;
            object oTrue = true;
            object oReplaceAll = Word.WdReplace.wdReplaceAll;
            object missing = System.Type.Missing;
            rngFind.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref oFindStop, ref oTrue, ref missing, ref oReplaceAll,
                ref missing, ref missing, ref missing, ref missing);
        }
    }

推荐阅读