1

我正在使用 word interop 编辑 word 文件。有时这些文件有多个“*”,我必须以编程方式替换它们。因此,如果有两个星号,我将用 no 替换它们。2.不知怎的,我找不到“*”。

 Word.Range rngAff = doc.Content;    
 rngAff.Find.ClearFormatting();
 rngAff.Find.Font.Superscript = 1;
 rngAff.Find.Text = @"\*{1,}";
 while (rngAff.Find.Execute())
            {
              rngAff.Find.Replacement.Text = this.CountReplaceAsterisks()
            }

不知何故执行命令不成功。如何在word文件中找到星号?

4

1 回答 1

1

当我需要替换 word 文件中的某些字符时,我正在使用那段代码,我得到了很好的结果:

class Program
{

  static void Main(string[] args)
  {
    SearchReplace();
  }


  private static void SearchReplace()
  {
    object missing = System.Reflection.Missing.Value;

    Application application = new Application();
    Microsoft.Office.Interop.Word.Document document = application.Documents.Add("C:\\Users\\test\\Desktop\\word.docx");

    Microsoft.Office.Interop.Word.Find findObject = application.Selection.Find;
    findObject.ClearFormatting();
    findObject.Text = "**";
    findObject.Replacement.ClearFormatting();
    findObject.Replacement.Text = "";

    object replaceAll = Microsoft.Office.Interop.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);

    object filename = "C:\\savefile.docx";
    document.SaveAs2(ref filename);;

    document.Close(ref missing, ref missing, ref missing);
    document = null;
    application.Quit(ref missing, ref missing, ref missing);
    application = null;
  }

}
于 2020-04-18T15:28:56.080 回答