ドキュメントが開いていないときにフォントの色を設定します

StackOverflow https://stackoverflow.com/questions/19841777

  •  29-07-2022
  •  | 
  •  

質問

そこで、私は各グループが3つある人々のためのドキュメントの作成に取り組んでいます。関係者全員のリストを含むVBAスクリプトを書き、コンピューターと一緒にいるグループにログインした人を識別できます。ただし、フォントの色を設定することはできません。 VBAスクリプトを記録し、フォントの色を設定して、単語がどのように機能するかを確認しましたが、結果のコードは 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
役に立ちましたか?

解決

AFAIK、特定のユーザーにデフォルトのフォント色を設定することはできません。たとえば、たとえば青に設定して、赤い色の文に移動しても、何かを入力した場合、青いテキストではなく赤いテキストが表示されます。カーソルがある位置は、その文の色付けに使用された元の色を選ぶからです。

ユーザーに特定の色を設定するには、どちらも必要です

  1. Identify a range and set the color for it. 。しかし、上で述べたように、ユーザーが別の範囲に移動する場合、新しい色設定はそこに適用されません。

  2. Set it for the entire document. 。ドキュメント全体に設定した場合、ドキュメント全体の色が変更され、これはあなたが望むものではないと確信しています。

他のヒント

可能な回避策のベース Application.WindowSelectionChange Event. 。したがって、次の手順を維持する必要があります。

1.クラスモジュールを作成します
2.クラスモジュールを名前を付けます App
3.次のコードを追加します 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.標準モジュールでパブリック変数を追加します。

    Public tmpApp As New App

5.標準モジュールでサブを作成するか、コードを追加します Document_Open Event, 、クラスを初期化します。

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.上記のサブを1回実行するか、コードが追加された場合にドキュメントを開きます Document_open event.

- -編集 - - (以下の@SIDからのいくつかのコメントの後)

提案されたソリューションを使用することには不便があります。しかし、それらのほとんどはいくつかを追加することで解決できます If statements 中身 WindowSelectionChange event. 。の位置を確認します Sel range パラメーター、周りのテキスト、その他を使用すると、新しい色を適用すべきかどうかを正確に決定できます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top