Jeffr.Lipton
Registered User.
- Local time
- Today, 09:56
- Joined
- Sep 14, 2018
- Messages
- 31
I'm using Access to change value in a Word document and I want to change the font color to red for numbers less than zero. Here's my calling code:
Here's my subroutine:
Here's the Word macro which works as expected:
I was able to find a work-around. Since all the text I want to change has a pattern of "(-9*)", I add this code after the last replace:
I'd still like to know why the original code didn't change the font color.
Code:
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Activate
Set doc = oWord.Documents.Open(fpath & "AF Final Costs Notification template.docx", True)
Set oSelection = oWord.Documents(1).Content
oSelection.Select
Set sel = oWord.Selection
Source_Text = "[EstProjCost_AF]"
Replacement_Text = Format(Me.EstProjCost_IF, "currency")
Call Replace_Text(sel, Source_Text, Replacement_Text)
Code:
Private Sub Replace_Text(sel, Source_Text, Replacement_Text)
Replacement_Text = Nz(Replacement_Text, "--NULL--")
With sel
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
With .Find
.Text = Source_Text
.Replacement.Font.Color = wdColorBlack
If IsNumeric(Replacement_Text) Then
If Replacement_Text < 0 Then
.Replacement.Font.Color = wdColorRed
End If
End If
.Replacement.Text = Replacement_Text
.Forward = True
.Wrap = 1 'wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
sel.Find.Execute Replace:=2 'wdReplaceAll
End With
End With
End Sub
Code:
Sub change_font() Dim Replacement_Text As String
'
' change_font Macro
'
'
Replacement_Text = "def"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
If IsNumeric(Replacement_Text) Then
If CInt(Replacement_Text) < 0 Then
Selection.Find.Replacement.Font.Color = wdColorRed
End If
End If
With Selection.Find
.Text = "abc"
.Replacement.Text = Replacement_Text
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Code:
With sel
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find.Replacement.Font.Color = wdColorRed
With .Find
.Text = "\(^#*\)"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
.Find.Execute Replace:=wdReplaceAll
End With
Last edited: