قواعد حلقة محتملة 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. Sub في الوحدة النمطية القياسية ، أو إضافة رمز إلى الخاص بك 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. قم بتشغيل SUB أعلاه مرة واحدة أو فتح المستند إذا تمت إضافة الرمز إلى Document_open event
.
---تعديل--- (بعد بعض التعليقات من sid أدناه)
هناك بعض المضايقات لاستخدام الحل المقترح. ومع ذلك ، يمكن حل معظمهم عن طريق إضافة البعض If statements
داخل WindowSelectionChange event
. التحقق من موقع Sel range
المعلمة والنص حولها والآخرون يسمحون بدقة ما إذا كان ينبغي تطبيق لون جديد أم لا.