Message-ID: <2130186662.8611.1711652056224.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_8610_1067799342.1711652056224" ------=_Part_8610_1067799342.1711652056224 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Search and Replace

Search and Replace

Intro

Demonstrates how to use= regular expressions with WordWriter to easily search and replace text.

This sample uses the WordApplication class to open an existing document on the serve= r and perform search and replace actions on it. First, view the stored docu= ment to determine which patterns to search for (search terms that work well= are WordWriter, and template). Remember that R= egular Expressions are case sensitive. So, to search for "WordWriter&q= uot; "Wordwriter", use this pattern: Word[Ww]riter.

Code

=20
        string SearchPattern =3D "[Ww]ord[Ww]riter";
        string replaceWith =3D "SoftArtisans";
        string docPath =3D @"..\..\WordTemplateFiles\SearchReplace.doc=
";

        /// <summary>
        /// Searches a document for a Regular Expression search term
        /// and replaces all instances of that term with a new string.
        /// The revised document is then saved.
        /// </summary>
        public void SearchDocument()
        {
            /// Open the document you wish to search

            WordApplication wordApp =3D new WordApplication();
            Document doc =3D wordApp.Open(docPath);

            // Execute the replacement by specifying the search term
            // as a Regular Expression string, and the replacement string.
            // SearchAndReplace will return the number of replacements made=
.

            int numReplacements =3D doc.SearchAndReplace(SearchPattern, rep=
laceWith);

            // If no occurrences of the search pattern were found in the do=
c, display
            // a message and return.

            if(numReplacements =3D=3D 0)
            {
                Console.WriteLine("Note: No occurrences of \"{0}\=
" were found in the document. " +
                                "Please try another search pattern.&qu=
ot;, SearchPattern);
                return;
            }

            // Prepend a message to the beginning of the document noting
            // how many replacements were made.
            string text =3D
                String.Format("Replaced {0} instances of \"{1}\&q=
uot; with \"{2}\"",
                    numReplacements, SearchPattern, replaceWith);

            // Create a new paragraph and insert the note
            Paragraph pg =3D doc.InsertParagraphBefore(null);
            pg.InsertTextAfter(text, false);

            // Save the edited document
            wordApp.Save(doc, @"..\..\WordOutputFiles\Replaced_out.doc=
");
        }
    }



=20

Downloads

Initial Document: Se= archReplace.doc

Output: SalesRepor= t_out.doc

------=_Part_8610_1067799342.1711652056224--