【问题标题】:c# find replace in wordc#在word中查找替换
【发布时间】:2018-05-03 02:43:12
【问题描述】:

我最近开始学习 C#。 我正在尝试用 C# 编写一些宏(用 VBA 编写) - 实际上我正在尝试为 Word 制作一个新的功能区,其中的按钮执行一些活动...... 所以,我正在尝试制作一个按钮,在活动文档中进行简单的查找/替换。 我从 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);
    }

这里有什么问题?

【问题讨论】:

  • 回到你复制的代码,复制缺失的声明。它可能来自单词 api 的一些空值。它带有下划线 - 嗯 - 因为它在您复制的代码中丢失。看这里会更快:msdn.microsoft.com/en-us/library/… 然后写下这个问题 - 先研究再问 SO :) api 这个词因方法的大量参数而臭名昭著,大部分时间都可能被“遗漏” -即没有填充合理的值
  • 其实,在问之前,我花了很长时间研究。你帮了很多忙,我添加了 object missing = Type.Missing;

标签: c# replace ms-word find


【解决方案1】:

在旧版本的 C# 中需要Type.Missing。在当前版本中,可以简化:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 2017-10-20
    • 1970-01-01
    • 2017-11-17
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    相关资源
    最近更新 更多