所以我正在为一些人创建一个文档,其中每个组(其中三个)被分配了一种字体颜色,以便他们输入到文档中。我编写了一个 VBA 脚本,其中包含所有相关人员的列表,并且可以识别登录到计算机的人员和他们所在的组。但是,我无法让字体颜色自行设置。我记录了一个 VBA 脚本,我在其中设置了字体颜色以查看 Word 是如何做到的,但是Selection.Font.Color = wdColorRed
当我将其添加到我的 VBA 脚本时,生成的代码实际上不会改变所选的字体颜色。这是我正在使用的代码示例:
Private Sub Document_Open()
Dim Users As New Scripting.Dictionary
Dim UserID As String
Dim category As String
UserID = GetUserName 'Currently using the example at
'http://support.microsoft.com/kb/161394 as a function
'---Add Members of Group 1---
Users.Add "person1", "group1"
Users.Add "person2", "group1"
'---Add Members of Group 2---
Users.Add "person3", "group2"
Users.Add "person4", "group2"
Users.Add "person5", "group2"
'---Add Members of Group 3---
Users.Add "person6", "group3"
Users.Add "person7", "group3"
For Each user In Users.Keys
If user = UserID Then
If Users.Item(user) = "group1" Then
Selection.Font.Color = wdColorRed
ElseIf Users.Item(user) = "group2" Then
Selection.Font.Color = wdColorGreen
ElseIf Users.Item(user) = "group3" Then
Selection.Font.Color = wdColorBlue
Else
Selection.Font.Color = wdColorBlack
End If
End If
Next
End Sub