tindavid
asked on
How to set different font and color for a text in Word document from Excel macro
I have a macro that can change a text in a Word doc by following code:
Dim wrdApp As New Word.Application
Dim wrdDoc As Word.Document
Application.DisplayAlerts = True
Application.EnableEvents = False
Application.ScreenUpdating = True
Set wrdApp = CreateObject("Word.Applica tion")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(temp late_doc)
Dim wrdRange As Word.Range
release_version = Range("E4").Value
Result = wrdDoc.Content.Find.Execut e(FindText :="{releas e_version} ", ReplaceWith:=release_versi on, Replace:=Word.WdReplace.wd ReplaceAll , ....... more setting ?)
Noted: {release_version} is the key word in Word doc
:release_version is the variable in macro
Now I want to set different color and fon size under condition of:
if release_version > 10 then
set text of release_version to Red and font size of 12
else
set text of release_version to Green and font size of 10
end if
Result = wrdDoc.Content.Find.Execut e(FindText :="{releas e_version} ", ReplaceWith:=release_versi on, Replace:=Word.WdReplace.wd ReplaceAll )
Dim wrdApp As New Word.Application
Dim wrdDoc As Word.Document
Application.DisplayAlerts = True
Application.EnableEvents = False
Application.ScreenUpdating
Set wrdApp = CreateObject("Word.Applica
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(temp
Dim wrdRange As Word.Range
release_version = Range("E4").Value
Result = wrdDoc.Content.Find.Execut
Noted: {release_version} is the key word in Word doc
:release_version is the variable in macro
Now I want to set different color and fon size under condition of:
if release_version > 10 then
set text of release_version to Red and font size of 12
else
set text of release_version to Green and font size of 10
end if
Result = wrdDoc.Content.Find.Execut
ASKER
Dear Graharm,
Thank you for your reply, the code of changing Font is working but not with the color
It run thru all codes but did not change the color, why?
Many thanks,
Following are my code: (last 2 comment outed lines are my original code)
If this_redo_size > base_redo_size Then
With wrdDoc.Content.Find
.Text = "{base_redo_size}"
.Replacement.Text = base_redo_size
.Replacement.Font.Color = wrdColourSeaGreen
.Replacement.Font.Size = 12
Result = .Execute(Replace:=Word.WdR eplace.wdR eplaceAll)
End With
With wrdDoc.Content.Find
.Text = "{this_redo_size}"
.Replacement.Text = this_redo_size
.Replacement.Font.Color = wrdColourRed
.Replacement.Font.Size = 12
Result = .Execute(Replace:=Word.WdR eplace.wdR eplaceAll)
End With
Else
With wrdDoc.Content.Find
.Text = "{base_redo_size}"
.Replacement.Text = base_redo_size
.Replacement.Font.Color = wrdColourRed
.Replacement.Font.Size = 12
Result = .Execute(Replace:=Word.WdR eplace.wdR eplaceAll)
End With
With wrdDoc.Content.Find
.Text = "{this_redo_size}"
.Replacement.Text = this_redo_size
.Replacement.Font.Color = wdColorSeaGreen
.Replacement.Font.Size = 12
Result = .Execute(Replace:=Word.WdR eplace.wdR eplaceAll)
End With
End If
'Result = wrdDoc.Content.Find.Execut e(FindText :="{base_r edo_size}" , ReplaceWith:=base_redo_siz e, Replace:=Word.WdReplace.wd ReplaceAll )
'Result = wrdDoc.Content.Find.Execut e(FindText :="{this_r edo_size}" , ReplaceWith:=this_redo_siz e, Replace:=Word.WdReplace.wd ReplaceAll )
Thank you for your reply, the code of changing Font is working but not with the color
It run thru all codes but did not change the color, why?
Many thanks,
Following are my code: (last 2 comment outed lines are my original code)
If this_redo_size > base_redo_size Then
With wrdDoc.Content.Find
.Text = "{base_redo_size}"
.Replacement.Text = base_redo_size
.Replacement.Font.Color = wrdColourSeaGreen
.Replacement.Font.Size = 12
Result = .Execute(Replace:=Word.WdR
End With
With wrdDoc.Content.Find
.Text = "{this_redo_size}"
.Replacement.Text = this_redo_size
.Replacement.Font.Color = wrdColourRed
.Replacement.Font.Size = 12
Result = .Execute(Replace:=Word.WdR
End With
Else
With wrdDoc.Content.Find
.Text = "{base_redo_size}"
.Replacement.Text = base_redo_size
.Replacement.Font.Color = wrdColourRed
.Replacement.Font.Size = 12
Result = .Execute(Replace:=Word.WdR
End With
With wrdDoc.Content.Find
.Text = "{this_redo_size}"
.Replacement.Text = this_redo_size
.Replacement.Font.Color = wdColorSeaGreen
.Replacement.Font.Size = 12
Result = .Execute(Replace:=Word.WdR
End With
End If
'Result = wrdDoc.Content.Find.Execut
'Result = wrdDoc.Content.Find.Execut
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dear Graham,
Yes, I am able to color it. Just one small question.
Since I have more than 20 different set of values to be compared and need to set different color. therefore I was trying to put above code into a function, however, every time when the code up to [Marked ABORT below] will abort. Do you think that I cannot make such call ? Here is my function call and main body
Function set_color(base_text As String, base_value As Long, this_text As String, this_value As Long)
If this_value > base_value Then
With wrdDoc.Content.Find ' ALWAYS abort after this line of code - ABORT HERE
.Text = base_text
.Replacement.Text = base_value
.Replacement.Font.Color = wdColorSeaGreen
.Replacement.Font.Size = 10
Resul = .Execute(Replace:=Word.WdR eplace.wdR eplaceAll)
End With
With wrdDoc.Content.Find
.Text = this_text
.Replacement.Text = this_value
.Replacement.Font.Color = wdColorRed
.Replacement.Font.Size = 10
Result = .Execute(Replace:=Word.WdR eplace.wdR eplaceAll)
End With
Else
With wrdDoc.Content.Find
.Text = base_text
.Replacement.Text = base_value
.Replacement.Font.Color = wdColorRed
.Replacement.Font.Size = 10
Result = .Execute(Replace:=Word.WdR eplace.wdR eplaceAll)
End With
With wrdDoc.Content.Find
.Text = this_text
.Replacement.Text = this_value
.Replacement.Font.Color = wdColorSeaGreen
.Replacement.Font.Size = 10
Result = .Execute(Replace:=Word.WdR eplace.wdR eplaceAll)
End With
End If
End Function
sub main()
.......
........
base_text = "base_redo_size}"
base_value = base_redo_size
this_text = "this_redo_size}"
this_value = this_redo_size
Result = set_color(base_text, base_value, this_text, this_value)
Yes, I am able to color it. Just one small question.
Since I have more than 20 different set of values to be compared and need to set different color. therefore I was trying to put above code into a function, however, every time when the code up to [Marked ABORT below] will abort. Do you think that I cannot make such call ? Here is my function call and main body
Function set_color(base_text As String, base_value As Long, this_text As String, this_value As Long)
If this_value > base_value Then
With wrdDoc.Content.Find ' ALWAYS abort after this line of code - ABORT HERE
.Text = base_text
.Replacement.Text = base_value
.Replacement.Font.Color = wdColorSeaGreen
.Replacement.Font.Size = 10
Resul = .Execute(Replace:=Word.WdR
End With
With wrdDoc.Content.Find
.Text = this_text
.Replacement.Text = this_value
.Replacement.Font.Color = wdColorRed
.Replacement.Font.Size = 10
Result = .Execute(Replace:=Word.WdR
End With
Else
With wrdDoc.Content.Find
.Text = base_text
.Replacement.Text = base_value
.Replacement.Font.Color = wdColorRed
.Replacement.Font.Size = 10
Result = .Execute(Replace:=Word.WdR
End With
With wrdDoc.Content.Find
.Text = this_text
.Replacement.Text = this_value
.Replacement.Font.Color = wdColorSeaGreen
.Replacement.Font.Size = 10
Result = .Execute(Replace:=Word.WdR
End With
End If
End Function
sub main()
.......
........
base_text = "base_redo_size}"
base_value = base_redo_size
this_text = "this_redo_size}"
this_value = this_redo_size
Result = set_color(base_text, base_value, this_text, this_value)
Is there an error message? If so what is it?
ASKER
Hi Graham,
With wrdDoc.Content.Find <-- error code (err.number) of 424 with no description
With wrdDoc.Content.Find <-- error code (err.number) of 424 with no description
That is an 'Object required' error.
Is the wrdDoc object variable definition within the scope of that code, and is it still instantiated?
Also do you have Option Explicit in the declarations section to catch undefined variables at compile time?
Is the wrdDoc object variable definition within the scope of that code, and is it still instantiated?
Also do you have Option Explicit in the declarations section to catch undefined variables at compile time?
ASKER
Excellent solution, thank you Graham.
Open in new window