我正在使用Access来更改Word文档中的值,我想将字体颜色更改为红色,以获得小于零的数字。这是我的调用代码:
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)
这是我的子程序:
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
任何帮助,将不胜感激。
ETA:这是按预期工作的Word宏: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
我找到了解决办法。由于我要更改的所有文本都具有“(-9999.99)”模式,因此我在最后一次替换后添加了此代码:
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
我仍然想知道为什么原始代码不起作用,但也许有一个更好的论坛要求...