【问题标题】:Access VBA not changing Word color访问 VBA 不更改 Word 颜色
【发布时间】: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 中运行良好(参见添加的宏)。

标签: ms-access vba


【解决方案1】:

我能够找到解决方法。由于我要更改的所有文本都有“(-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

我仍然想知道为什么原始代码不起作用,但也许有更好的论坛可以在...上提问...

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多