可能的解决方法基础 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.如果将代码添加到 Document_open event
.
- -编辑 - - (在下面@SID的一些评论之后)
使用建议的解决方案有一些不便。但是,大多数可以通过添加一些来解决 If statements
里面 WindowSelectionChange event
. 。检查位置 Sel range
参数,周围的文本和其他内容允许精确确定是否应应用新颜色。