cancel
Showing results for 
Search instead for 
Did you mean: 

Ms word highlight text colour to red

Rama_krishna
Level 4

Hi all,

i have written a table in ms word now i want change some text colour to red or green 

example in below table i want to change colour

  •   www text colour to red and 
  •    yyy text colour to green.

need solution how to achieve.

Rama_krishna_0-1722491378307.png

 

4 REPLIES 4

faheemsd
MVP

Dear @Rama_krishna 

Could you please create a custom code with the below in the MS Word VBO

Sub ChangeTextColor()
Dim doc As Document
Dim rng As Range

' Set the document object
Set doc = ThisDocument

' Change "www" text color to red
Set rng = doc.Content
With rng.Find
.Text = "www"
.Replacement.Text = "www"
.Replacement.Font.Color = wdColorRed
.Execute Replace:=wdReplaceAll
End With

' Change "yyy" text color to green
Set rng = doc.Content
With rng.Find
.Text = "yyy"
.Replacement.Text = "yyy"
.Replacement.Font.Color = wdColorGreen
.Execute Replace:=wdReplaceAll
End With
End Sub

Could you please implement the above and let me know the results.

Denis__Dennehy
Level 15

I'd probably just say that rather than amending the official MS Word VBO, best practice is to create a separate VBO with any new actions (plus the connect/attach actions so you can use it).  Not amending the official word VBO will make it easer to take newer versions of it in future.

Hi @faheemsd below error i am getting

'Let' and 'Set' assignment statements are no longer supported.

Dear @Rama_krishna 

I apologies for the delay in responding; my project work kept me quite busy.

I have made the code so simple that you may use any color to apply it to any text that is included in the Word document.

faheemsd_0-1723927539019.png

Input Parameters:

handle

document name
textTocolor

colorNumber

faheemsd_1-1723927644853.png

Code used in Code stage

' Find and replace text color
Dim d As Object = GetDocument(handle,document_name)
Dim r As Object = d.Range

' Execute find and replace
Do While r.Find.Execute(textTocolor)
r.Font.Color = colorNumber ' wdColorRed
r.Start = r.End
Loop
' Release objects
r = Nothing

for your requirement
red color number= 225 
Green color number = 5287936

faheemsd_1-1723928089377.png

faheemsd_2-1723928121088.pngfaheemsd_3-1723928158089.png

faheemsd_4-1723928275683.png

faheemsd_5-1723928293470.png

This is how we can achieve adding the colors to the required text in Word Document using Blue Prism
Please let me know if you face any difficulties Implementing the above steps.
@Michael_S  All Blue Prism users will find this code useful in changing the text color in Word documents.