I am editing word files using word interop. Sometimes these files have multiples "*" which I have to replace programmatically. So, if there are two asterisks I will replace them with no. 2. Somehow I am not able to find the "*".
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()
}
Somehow execute command is not successful. How to I find asterisks in word file?
When i need to replace some character in a word file, i am using that piece of code, and i have a good result:
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;
}
}