question

IProfileTNo-6711 avatar image
0 Votes"
IProfileTNo-6711 asked ·

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

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

Hi,

Welcome to our Microsoft Q&A platform!

You can try to add a space like this:

 object findStr3 = "No ";

Thanks.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Alex Already tried "No " and " No" and it does not make any action on the document

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

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

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ok i will try it thanks

0 Votes 0 ·

it works but it keeps on targeting other things like

No.1
No.2

things that has "No."

0 Votes 0 ·
AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

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.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.