1

我正在尝试弄清楚如何在 word 文件中找到所有红色文本(然后将其删除)。然而,我似乎走入了死胡同。有人可以帮我找到正确的方向吗?

这是我到目前为止所拥有的:

public Form1()
    {
        InitializeComponent();

        object missing = System.Reflection.Missing.Value;
        object readOnly = false;
        object isVisible = true;
        object fileName = "D:\\word.docx";

        Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
        Document document = application.Documents.Open(@"D:\word2.docx",missing,true,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);

        FindAndReplace(application, "", "");
    }

    private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
    {
        //options
        object matchCase = false;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object matchAllWordForms = false;
        object forward = true;
        object format = true;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        doc.Selection.Find.ClearFormatting();
        doc.Selection.Find.Replacement.ClearFormatting();
        doc.Selection.Find.Replacement.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;

        //execute find and replace
        doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
            ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
    }

当我运行它时,我得到一个 COM 异常说

该命令不可用。

4

0 回答 0