im using word Inetrop in a WPF C# App Which i find a word and Change its color to red and im facing issues

IProfileTNo 1 Reputation point
2020-04-08T01:23:51.503+00:00

This is my Code it works but I have a problem at the third part when I try to change the word 'No' color to red it changes any word that includes 'No' for example 'Not Isolated' was Effected by this and its color was changed

how can i specify that i want only the word 'No' to be red?

object findStr = 'Not Applicable';
object findStr2 = 'N/A';
object findStr3 = 'No';

while (fileOpen.Selection.Find.Execute(ref findStr))
{

fileOpen.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;
}
fileOpen.Selection.Start = 0;
while (fileOpen.Selection.Find.Execute(ref findStr2))
{

fileOpen.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;
}
fileOpen.Selection.Start = 0;
while (fileOpen.Selection.Find.Execute(ref findStr3))
{

fileOpen.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;

}

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-04-08T05:25:37.047+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

    You can try to add a space like this:

    object findStr3 = "No ";
    

    Thanks.


  2. Viorel 118K Reputation points
    2020-04-08T14:52:55.673+00:00

    Maybe execute ‘fileOpen.Selection.Find.MatchWholeWord = True’ before starting the loops.


  3. Alex Li-MSFT 1,096 Reputation points
    2020-04-15T01:33:15.183+00:00

    But I tried "No ",it works well.Please see my code:

     private void Button_Click(object sender, RoutedEventArgs e)  
                {  
                     
                    MSWord.Application wordApp = new MSWord.Application();  
                    MSWord.Document wordDoc;         
                    wordApp = new MSWord.Application();   
                    wordApp.Visible = false;  
                    Object Nothing = Missing.Value;  
                    wordDoc = wordApp.Documents.Open(Environment.CurrentDirectory + "\\test.docx", ref Nothing, ref Nothing, ref Nothing, ref Nothing);  
                    //object findStr1 = "Not Applicable";  
                    //object findStr2 = "N/A";  
                    object findStr3 = "No ";  
          
                    //while (wordApp.Selection.Find.Execute(ref findStr1))  
                    //{  
                    //    wordApp.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;  
          
                    //}  
          
                    //wordApp.Selection.Start = 0;  
                    //while (wordApp.Selection.Find.Execute(ref findStr2))  
                    //{  
          
                    //    wordApp.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlack;  
          
                    //}  
          
                    //wordApp.Selection.Start = 0;  
                    //wordApp.Selection.Find.MatchWholeWord = true;  
                    while (wordApp.Selection.Find.Execute(ref findStr3))  
                    {  
                          
                        wordApp.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;  
                    }  
          
                    wordDoc.Save();  
                    wordDoc.Close(false, Nothing, Nothing);  
                    wordApp.Quit(false, false, false);  
          
                }  
      
    

    7368-annotation-2020-04-15-093207.png

    Thanks.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.