Question

So I'm working on creating a document for some folks where each group, of which there are three, is assigned a font color for their input into the document. I've written a VBA script that contains a list of everyone involved and can identify the person logged into the computer and the group they are with. However, I am unable to get the font color to set itself. I recorded a VBA script where I set the font color to see how Word does it, but the resulting code of Selection.Font.Color = wdColorRed won't actually change the selected font color when I add it to my VBA script. Here is an example of the code that I'm using:

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
Was it helpful?

Solution

AFAIK, You can't set a default font color for a specific user. Even if you managed to, say, set it to Blue and you navigate to a sentence which is in red color and if you typed anything, you will not see blue text but red text. Because the position where the cursor is will pick the original color that was used to color that sentence.

To set a specific color for a user, you will either have to either

  1. Identify a range and set the color for it. But then like I mentioned above, if the user navigates to a different range then the new color setting will not apply there.

  2. Set it for the entire document. If you set it for the entire document then the color of the entire document will change and I am sure this is not what you want.

OTHER TIPS

Possible workaround bases on Application.WindowSelectionChange Event. Therefore you need to keep the following steps:

1.Create Class module
2.Name your Class module App
3.Add the following code to App Class Module:

    Public WithEvents WRD As Application

    Private Sub WRD_WindowSelectionChange(ByVal Sel As Selection)
        'here you should place solution from your Document_Open sub
        'which defines color based on user name or...
        'you could place it somewhere else but pass color value 
        'here as a parameter

        'for test I just assumed that color should be blue
        Sel.Font.Color = wdColorBlue
    End Sub

4.In standard module add public variable:

    Public tmpApp As New App

5.Create Sub in standard module, or add code to your Document_Open Event, which will initialize our class:

Sub Document_Open_new()

    Set tmpApp.WRD = Application

    'we need to change selection once to trigger full
    'scope of solution
    'if we omit the code below new color will not work
    'if user add new text at the beginning of the document
    'right after it was opened
    Dim tmpSel As Range
    Set tmpSel = Selection.Range
    ActiveDocument.Bookmarks("\EndOfDoc").Select
    tmpSel.Select
End Sub

6.Run the above sub once or open document if the code was added to Document_open event.

---EDIT--- (after some comments from @Sid below)

There are some inconveniences of using proposed solution. However, MOST OF THEM COULD BE SOLVED by adding some If statements inside WindowSelectionChange event. Checking for position of Sel range parameter, text around and others allows to precisely decide whether new color should be applied or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top