Word inerop c#:在 word 文件中查找和替换星号
Word inerop c#: Finding and replacing asterisk in word file
我正在使用 word interop 编辑 word 文件。有时这些文件有多个“*”,我必须以编程方式替换它们。所以,如果有两个星号,我会将它们替换为否。 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文件中找到星号?
当我需要替换一个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;
}
}
我正在使用 word interop 编辑 word 文件。有时这些文件有多个“*”,我必须以编程方式替换它们。所以,如果有两个星号,我会将它们替换为否。 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文件中找到星号?
当我需要替换一个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;
}
}