C# 如何使用interop c以编程方式清除word中的特定文本格式#

C# 如何使用interop c以编程方式清除word中的特定文本格式#,c#,windows,office-interop,office-automation,C#,Windows,Office Interop,Office Automation,我已经创建了一个windows应用程序,我们正在其中自动化MsOffice 目前我正在研究“清除文本上的所有格式”。此方法将清除word中特定文本的格式,并设置默认字体及其大小 以下是我迄今为止尝试的代码: public static bool ClearAllFormatting(IQuestion question, string filename, string text ) { WordInterop.Application wordApplication = G

我已经创建了一个windows应用程序,我们正在其中自动化MsOffice

目前我正在研究“清除文本上的所有格式”。此方法将清除word中特定文本的格式,并设置默认字体及其大小

以下是我迄今为止尝试的代码:

public static bool ClearAllFormatting(IQuestion question, string filename, string text )
    {
        WordInterop.Application wordApplication = GetOrCreateWordApplication(question.ObjectStore);

        try
        {
            //Avoid screen flickering or unwanted alerts while initializing
            wordApplication.ScreenUpdating = false;
            WordInterop.WdAlertLevel displayAlertLevel = wordApplication.DisplayAlerts;
            wordApplication.DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone;

            WordInterop.Document wordDocument = wordApplication.Documents.Open(filename);
            WordInterop.Paragraph para = wordDocument.Paragraphs[1];
            WordInterop.Range range = para.Range;
            if(range.Find.Execute(text))
            {
                range.Find.ClearFormatting();

            }


            object missing = Type.Missing;

            wordDocument.Save();
            object save_changes = false;
            wordDocument.Close(ref save_changes, ref missing, ref missing);
            return true;
        }
        catch (Exception)
        {
            Cleanup(question.ObjectStore, true);
        }
        return false;
    }
但是,不幸的是,这种方法没有清除文本上的格式

谁能帮帮我吗


谢谢

ClearFormatting将删除搜索到的格式。它不会从文档本身删除任何格式。请解决此问题