在Word

时间:2019-06-20 11:08:35

标签: c# ms-word office-interop word-interop

我试图弄清楚如何在一个单词文件中找到所有红色文本(然后将其删除)。但是,我似乎已经走到了尽头。有人可以帮我找对方向吗?

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

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异常,表示

  

该命令不可用。

0 个答案:

没有答案