如何删除单词中的说明文字?

时间:2017-06-21 14:26:27

标签: c#

如何删除单词中的说明文字?

我从早上起床了。请有人帮助我。这是我的代码。

Microsoft.Office.Interop.Word.Application app = Globals.ThisAddIn.Application;

Microsoft.Office.Interop.Word.Document doc1 = app.ActiveDocument;
Selection wordSelection = Globals.ThisAddIn.Application.Selection;
wordSelection.HomeKey(WdUnits.wdStory);

doc1.Content.Find.ClearFormatting();
doc1.Content.Find.Replacement.ClearFormatting();
doc1.Content.Find.set_Style(doc1.Styles["Instructions"]);
doc1.Content.Find.Text = "";
doc1.Content.Find.Replacement.Text = "";
doc1.Content.Find.Forward = true;

bInstructions = doc1.Content.Find.Execute(WdReplace.wdReplaceAll);

1 个答案:

答案 0 :(得分:1)

尝试使用基于Selection对象的Find对象:

    Microsoft.Office.Interop.Word.Selection wordSelection = app.ActiveWindow.Selection;
    wordSelection.HomeKey(WdUnits.wdStory);
    Find fnd = wordSelection.Find;

    fnd.ClearFormatting();
    fnd.Replacement.ClearFormatting();
    fnd.Forward = true;
    fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    fnd.Text = "";
    fnd.Replacement.Text = "";
    fnd.set_Style("Instructions");
    fnd.Execute(Replace: WdReplace.wdReplaceAll);

请注意,执行命令的签名是:

bool Execute(
ref Object FindText, ref Object MatchCase, ref Object MatchWholeWord,
ref Object MatchWildcards, ref Object MatchSoundsLike, ref Object MatchAllWordForms, ref Object Forward, ref Object Wrap, ref Object Format,
ref Object ReplaceWith, ref Object Replace, ref Object MatchKashida,
ref Object MatchDiacritics, ref Object MatchAlefHamza, ref Object MatchControl)

默认值为每个缺失 - 在您的代码中,您将WdReplace.wdReplaceAll作为FindText参数传递,而不是传递多个Missing参数,我的代码使用命名参数选项。即使使用命名参数,您的代码也没有替换,所以我使用了当前文档的选择。