Search code examples
c#office-interopword-interop

Replace text in Word Interop 16


https://learn.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-search-for-and-replace-text-in-documents?view=vs-2019

Microsoft doesn't give full code in docs. But with this my code try below text haven't been replaced. What's wrong and how to make it to save replaced content to file named Word2.docx ?

using Microsoft.Office.Interop.Word;

namespace ConsoleApp1
{
    class Program
    {


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

        private static object missing; // <- Is this correct ?

        private static void SearchReplace()
        {
            Application application = new Application();
            application.Documents.Add("C:\\Users\\test\\Desktop\\word.docx");

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

            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);

            application.Documents.Save();
        }

    }
}

Solution

  • I think you have a problem with missing definition and i have added the method to save:

    namespace ConsoleApp1
    {
        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 = "find me";
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = "Found";
    
            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;
        }
    
      }
    }