Skip to main content
 首页 » 编程设计

c#之如何在 Word 文件中查找数学方程式,如果找到则使用 vsto c# 突出显示它

2024年02月05日10TianFang

如何在 Word 文件中找到数学公式?

请提出相同的建议。

我的输出会变成这样:

请您参考如下方法:

使用下面的 C# 代码以黄色突出显示所有 MathType 方程。 在使用此代码之前,请在类文件的命名空间声明中添加 using Word = Microsoft.Office.Interop.Word;。

public bool FindAndHighlightMathtypeEquation(ref Word.Range myRange) 
    { 
        try 
        { 
            int inlineShapesCount = myRange.InlineShapes.Count; 
            if (inlineShapesCount > 0) 
            { 
                for (int i = 1; i <= inlineShapesCount; i++) 
                { 
                    Word.InlineShape currentShape = myRange.InlineShapes[i]; 
                    Word.Range currentShapeRange = currentShape.Range; 
                    Word.WdInlineShapeType typeOfCurrentShape = currentShape.Type; 
 
                    if (typeOfCurrentShape != Word.WdInlineShapeType.wdInlineShapeEmbeddedOLEObject) 
                    { 
                        continue; 
                    } 
 
                    if (!currentShape.Field.Code.Text.Trim().ToLower().Contains("equation")) 
                    { 
                        continue; 
                    } 
 
                    currentShapeRange.Select(); 
                    currentShapeRange.Application.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdYellow; 
                } 
            } 
 
            MessageBox.Show("Process Completed"); 
 
        } 
        catch (Exception) 
        { 
            throw; 
        } 
        return true; 
    }