MS Word VBA 无法 select 特定单词更改背景颜色
MS Word VBA Can't select specific words to change background color
我正在尝试编写 vba 代码来更改特定单词的背景颜色,但没有成功。
我有可以改变背景颜色的代码,但当我要求输入特定单词时不会。
我有 SQL 代码,我试图让查询中的所有字段都以亮绿色突出显示,所以我的计划是只输入字段名称并让这些术语以亮绿色突出显示。
目前,如果我取出 if 语句,第一行将以亮绿色突出显示。
我有超过 3200 个单词要完成。
Dim oSentence As Variant
Dim strSentence As String
strSentence = "Package_Policy_TA"
For Each oSentence In ActiveDocument.Sentences
Debug.Print oSentence
If oSentence = strSentence Then
Stop
oSentence.Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
End If
Next oSentence
例如:
Sub HiliteWords()
Application.ScreenUpdating = False
Dim ArrFnd As Variant, i As Long
'Array of Find expressions
ArrFnd = Array("policy_Id", "policy_payer_option", "rpt_coverage", "coverage_id")
With ActiveDocument
For i = 0 To UBound(ArrFnd)
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchCase = True
.MatchWholeWord = True
.Text = ArrFnd(i)
.Replacement.Text = ""
End With
Do While .Find.Execute
.Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
.Collapse wdCollapseEnd
Loop
End With
Next
End With
Application.ScreenUpdating = True
End Sub
我正在尝试编写 vba 代码来更改特定单词的背景颜色,但没有成功。 我有可以改变背景颜色的代码,但当我要求输入特定单词时不会。 我有 SQL 代码,我试图让查询中的所有字段都以亮绿色突出显示,所以我的计划是只输入字段名称并让这些术语以亮绿色突出显示。 目前,如果我取出 if 语句,第一行将以亮绿色突出显示。 我有超过 3200 个单词要完成。
Dim oSentence As Variant
Dim strSentence As String
strSentence = "Package_Policy_TA"
For Each oSentence In ActiveDocument.Sentences
Debug.Print oSentence
If oSentence = strSentence Then
Stop
oSentence.Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
End If
Next oSentence
例如:
Sub HiliteWords()
Application.ScreenUpdating = False
Dim ArrFnd As Variant, i As Long
'Array of Find expressions
ArrFnd = Array("policy_Id", "policy_payer_option", "rpt_coverage", "coverage_id")
With ActiveDocument
For i = 0 To UBound(ArrFnd)
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchCase = True
.MatchWholeWord = True
.Text = ArrFnd(i)
.Replacement.Text = ""
End With
Do While .Find.Execute
.Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
.Collapse wdCollapseEnd
Loop
End With
Next
End With
Application.ScreenUpdating = True
End Sub