简体   繁体   English

C#查找替换单词

[英]c# find replace in word

I recently started learning C#. 我最近开始学习C#。 I'm trying to write some of my macros (written in VBA) in C# - actually I'm trying to make a new ribbon for Word with buttons doing some activities... So, I'm trying to make a button doing simple find/replace in active document. 我正在尝试用C#编写一些宏(用VBA编写)-实际上,我正在尝试使用一些按钮来为Word创建一个新的功能区,因此,我试图使按钮变得简单在活动文档中查找/替换。 I copied this code from Microsoft site, but it keeps underlining word MISSING: 我从Microsoft网站复制了此代码,但它始终强调单词MISSING:

public void button1_Click(object sender, RibbonControlEventArgs e){
        Word.Find findObject = Globals.c_sharp.Application.Selection.Find;
        findObject.ClearFormatting();
        findObject.Text = "find me";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = "Found";

        object replaceAll = Word.WdReplace.wdReplaceAll;
        findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref replaceAll, ref missing, ref missing, ref missing, ref missing);
    }

What's the problem here? 这是什么问题

Type.Missing is needed in older versions of C#. Type.Missing较旧版本中需要Type.Missing In current versions, it can be simplified: 在当前版本中,它可以简化:

Globals.c_sharp.Application.ActiveDocument.Content.Find.Execute("find me", 
               Format: false, ReplaceWith: "Found", Replace: Word.WdReplace.wdReplaceAll);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM