Highlighting of text(s) in Word Document

Hi

Good Day.

I currently have a case whereby the bot will read 2 documents -

  1. Word document of a passage
  2. Excel sheet, list of words to be picked out in the passage

The bot will run through the passage and see if any of the words listed in the excel sheet appears in the passage. If it does, we would want the bot to highlight the words and we will check it at the end of the process.

As I am quite new to RPA, I can’t seem to find a way to do it. Tried searching online for tutorials but unable to find suitable ones as well.

Really appreciate some guidance on this case please.

Thank you.

Can you be more specific in what you need help with?

Maybe this thread will help you: Change color to a text in Word document

Hi @Alearns

Please give the following approach a try:

  1. Keep the list of words in an excel file, read the file, get the data & store the list of words in a variable (array or list).
  2. Use Word Application Scope to open the word document & read the contents. Save it in a variable as well.
  3. Using ForEach, iterate through the list of words. Inside the loop, search for the word using .Contains() function. If the word is available -
    i. Use the Replace Text activity to highlight the word by wrapping it with some special characters such as and .
  4. By the end, use Set Text activity to keep the replaced word doc as the latest.

Hope this gives you an idea,
Best Regards.

Hi Eric

Appreciate your prompt response.

Ideally, it will be something like this:

I am able to do 1 & 2 but got stuck at the 3rd part.

Thanks for the link as well, will go through it.

Hi @arjunshenoy

Appreciate your prompt response and the help given.

Ideally, it will be something like this:

I will give your approach a try.

Thank you.

Hi @Alearns ,

Could you try to use the below Code Snippet in Invoke Code Activity (Language set to CSharp)

try 
{
	var wordApplication = new Microsoft.Office.Interop.Word.Application() { Visible = false };

	var myDocument = wordApplication.Documents.Open(in_FilePath);

	foreach(string word in in_WordsToColor)
	{
		Microsoft.Office.Interop.Word.Find find = myDocument.Content.Find;
		find.Execute(word);
		
		if (find.Found)
		{
	    	Microsoft.Office.Interop.Word.Range range =(Microsoft.Office.Interop.Word.Range) find.Parent;
	    	range.Select();
			myDocument.Application.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorRed;
		}
	}
	
	myDocument.Save();
	wordApplication.Quit();
	
}
catch( Exception e)
{
	Console.WriteLine(e.Source.ToString()+" "+e.Message.ToString()+" "+e.TargetSite.ToString());
}

Here, we would need to pass two arguments to Invoke Code Activity, in_FilePath which should be the file path of the Word Document and in_WordsToColor, an Array of String type which should contain the array/list of words that you would want to color.

Let us know if you are facing issues in performing the above.