Intrope Word

MNH ASP 101 Reputation points
2022-11-24T11:35:33.28+00:00

Hello Guys,

I have Word Document. I need to replace word by regex using intrope c# library i don't know how to do it.
Can you please suggest me. or provide code snippet for that....

pattern => ${word}

Thank you :)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,620 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2022-11-25T05:48:42.933+00:00

    @MNH ASP , Welcome to Microsoft Q&A, I suggest that you could use Find.MatchWildcards property (Word) to replace word by using regex in word file.

    Here is a code example you could refer to.

    I changed the words that start with Example to This is Example.

      static void Main(string[] args)  
            {  
                Word.Application app=new Word.Application();  
                Word.Document doc = app.Documents.Open("test1.docx");  
                object missing = Missing.Value;  
                Word.Find findObject = app.Selection.Find;  
                findObject.ClearFormatting();  
                findObject.Text = "Example@";  
                findObject.MatchWildcards = true;  
                findObject.Replacement.ClearFormatting();  
                findObject.Replacement.Text = "This is Example";  
      
                object replaceAll = 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);  
                doc.Save();  
                doc.Close();  
                  
                
            }  
    

    Result:

    264077-before.jpg
    264047-after.jpg

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful