简体   繁体   English

如何使用C#在Microsoft Word的文本框中使特定单词加粗?

[英]How to make specific words bold in a Text Box in Microsoft Word using C#?

I have an application that will print a .docx file, which has a Text Box shape with text. 我有一个应用程序将打印一个.docx文件,该文件具有带有文本的文本框形状。 In it, there are certain words that need to be bold. 其中某些单词需要加粗。

For example, a part of the text is "...representando a empresa M&A, o presente..." and only "M&A" needs to be bold. 例如,文本的一部分为“ ...代表并购企业并购,或...”,仅“ M&A”必须为粗体。

However, I don't know for sure how I can do this. 但是,我不确定如何做到这一点。 I've searched in SO, as well as other sites like MSDN and such, but none provide the solution. 我已经搜索了SO以及MSDN之类的其他站点,但没有一个提供解决方案。 Could anyone help me? 有人可以帮我吗?

EDIT: I'm using Interop to achieve this. 编辑:我正在使用Interop来实现这一目标。 The code I've done so far is as follows: 到目前为止,我完成的代码如下:

// 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; 

Thanks in advance! 提前致谢!

The code extract below demonstrates how to loop through all Shapes in a Word document, check whether it's a text box (drawing element) that begins with the desired string, then search the text box for the term to be bolded and bold it. 下面的代码摘录演示了如何循环遍历Word文档中的所有Shapes,检查它是否是以所需字符串开头的文本框(绘图元素),然后在文本框中搜索要加粗的术语并将其加粗。

  1. It's possible to check the Shape.Type to determine whether or not it's a text box. 可以检查Shape.Type以确定它是否是一个文本框。 The Shape.Type is an Office Enum (since the Shape object is common to many Office applications). Shape.Type是一个Office枚举(因为Shape对象是许多Office应用程序所共有的)。 So if (shapeType == Office.MsoShapeType.msoTextBox) 所以if (shapeType == Office.MsoShapeType.msoTextBox)

  2. To pick out text and format it, most often Word's Range.Find functionality is the best approach. 要挑选文本并设置其格式,通常是使用Word的Range.Find功能是最好的方法。 It enables to find and replace formatting, as well as string values. 它使查找和替换格式以及字符串值成为可能。 (Tip: When embarking on a project that requires this kind of thing it's often a good idea to test in the dialog box (Ctrl+H) in the Word UI to figure out the right combination of parameters.) (提示:启动需要此类操作的项目时,通常最好在Word UI的对话框(Ctrl + H)中进行测试,以找出正确的参数组合。)

  3. To understand what all the parameters of Find.Execute are for, consult the Help or look at Intellisense (or a combination of the two). 若要了解Find.Execute所有参数的Find.Execute ,请查阅“帮助”或查看Intellisense(或两者的组合)。

For this example, it's important to note that, in order to format the target text no Replacement.Text is specified. 对于此示例,重要的是要注意,为了格式化目标文本,未指定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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM