【发布时间】:2018-09-13 00:14:32
【问题描述】:
我正在使用 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 宏: 子 change_font() 将 Replacement_Text 调暗为字符串
'
' 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
【问题讨论】:
-
当前代码有什么问题?欢迎使用 Stackoverflow
-
欢迎来到 SO。请更具体地说明您的问题。您是否在 VBA 模块中激活了对 Word Library 的引用?此外,另一种选择可能是使用
.Replacement.Font.Color = vbRed而不是.Replacement.Font.Color = wdColorRed -
感谢您的回复。文本正在被替换,但始终为黑色,从不为红色。该宏在 Word VBA 中运行良好(参见添加的宏)。