مشاركة عبر


كيفية القيام بما يلي: إضافة العلامات الذكية إلى مستندات Word

ينطبق على

تنطبق المعلومات الموجودة في هذا الموضوع فقط على أنواع المشاريع وإصدارات Microsoft Office التالية: لمزيد من المعلومات، راجع الميزات المتوفرة بواسطة تطبيقات Office و نوع المشروع.

نوع المشروع

  • مشروعات على مستوى المستند

  • مشروعات على مستوى التطبيق

إصدار Microsoft Office

  • Word 2007

يمكنك إضافة علامات ذكية إلى مستندات Microsoft Office Word للتعرف على النص ومنح المستخدم الوصول إلى إجراءات متعلقة بالمصطلحات التى تم التعرف عليه. التعليمات البرمجية التي تكتبها لأجل إنشاء علامة ذكية وتكوينها هى نفسها للمشاريع على مستوى المستند وعلى مستوى التطبيق لكن توجد بعض الاختلافات في الطريقة التي يتم بها إقران العلامات الذكية بالمستندات. العلامات الذكية أيضاً لديها نطاق مختلف في المشاريع على مستوى المستند وعلى مستوى التطبيق.

يصف هذا الموضوع المهام التالية:

  • إضافة علامة ذكية باستخدام تخصيص على مستوى المستند

  • إضافة علامة ذكية عن طريق استخدام وظيفة إضافية على مستوى التطبيق

لتشغيل علامة ذكية، يجب أن يكون لدى المستخدمين علامات ذكية تم تمكينها في Word أو Excel. لمزيد من المعلومات، راجع كيفية القيام بما يلي: تمكين العلامات الذكية في Word وExcel.

إضافة علامة ذكية باستخدام تخصيص على مستوى المستند

العلامات الذكية في التخصيصات علي مستوي المستند يتم التعرف عليها فقط في المستند المقترن بالتخصيص فقط.

لإضافة علامة ذكية باستخدام تخصيص على مستوى المستند

  1. قم بإنشاء كائن SmartTag، وقم بتكوين هذا الكائن لتعرّف سلوك العلامة الذكية:

    • لتحديد النص الذي تريد أن تعرفه، قم باستخدام الخاصية Terms أو Expressions.

    • لتعريف الإجراءات التى يمكن للمستخدمين النقر فوقها على العلامة الذكية، قم بإضافة واحد أو أكثر من كائن Action إلى الخاصية Actions.

    لمزيد من المعلومات، راجع بنية العلامات الذكية.

  2. قم بإضافة SmartTag إلى الخاصية VstoSmartTags من الفئة ThisDocument.

مثال التعليمة البرمجية التالية يقوم بإنشاء علامة ذكية تتعرف على الكلمات term و recognize. عندما ينقر المستخدم فوق العلامة الذكية , فإنه يعرض مواضع أحرف البدء والانتهاء للكلمة التي تم التعرف عليها. لتشغيل هذه التعليمات البرمجية، قم بإضافة التعليمات البرمجية إلى الفئة ThisDocument ثم قم باستدعاء الأسلوب AddSmartTag من معالج الحدث ThisDocument_Startup.

ملاحظة

المثال التالي يعمل في مشاريع تستهدف .NET Framework 4. لاستخدام هذا المثال في المشاريع التي تستهدف NET Framework 3.5. راجع التعليقات الموجودة في التعليمات البرمجية.

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Word.SmartTag = Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

    ' For .NET Framework 3.5 projects, use the following code to create the smart tag.
    ' Dim smartTagDemo As New  _
    '     Microsoft.Office.Tools.Word.SmartTag( _
    '     "www.microsoft.com/Demo#DemoSmartTag", _
    '     "Demonstration Smart Tag")

    ' Specify the terms to recognize.
    smartTagDemo.Terms.Add("term")
    smartTagDemo.Terms.Add("recognize")

    ' Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

    ' For .NET Framework 3.5 projects, use the following code to create the action.
    ' displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() { _
            displayAddress}

    ' Add the smart tag.
    Me.VstoSmartTags.Add(smartTagDemo)
End Sub

Private Sub DisplayAddress_BeforeCaptionShow(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.BeforeCaptionShow

    Dim clickedAction As Microsoft.Office.Tools.Word.Action = _
        TryCast(sender, Microsoft.Office.Tools.Word.Action)

    If clickedAction IsNot Nothing Then
        clickedAction.Caption = "Display the location of " & e.Text
    End If
End Sub

Private Sub DisplayAddress_Click(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.Click

    Dim termStart As Integer = e.Range.Start
    Dim termEnd As Integer = e.Range.End
    MsgBox("The recognized text '" & e.Text & _
            "' begins at position " & termStart & _
            " and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // For .NET Framework 3.5 projects, use the following code to create the smart tag.
    // Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Word.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify the terms to recognize.
    smartTagDemo.Terms.Add("term");
    smartTagDemo.Terms.Add("recognize");

    // Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced"); 

    // For .NET Framework 3.5 projects, use the following code to create the action.
    // displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");

    // Add the action to the smart tag.
    smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { 
        displayAddress };

    // Add the smart tag.
    this.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
        displayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Word.ActionClickEventHandler(
        displayAddress_Click);
}

void displayAddress_BeforeCaptionShow(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    Microsoft.Office.Tools.Word.Action clickedAction =
        sender as Microsoft.Office.Tools.Word.Action;

    if (clickedAction != null)
    {
        clickedAction.Caption = "Display the location of " +
            e.Text;
    }
}

void displayAddress_Click(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    int termStart = e.Range.Start;
    int termEnd = e.Range.End;
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' begins at position " + termStart.ToString() +
        " and ends at position " + termEnd.ToString());
}

إضافة علامة ذكية عن طريق استخدام وظيفة إضافية على مستوى التطبيق

عندما تقوم بإضافة علامة ذكية باستخدام وظيفة إضافية علي مستوى التطبيق ، يمكنك تحديد ما إذا كنت ترغب في جعل العلامات الذكية تعمل فقط في مستند أو في كافة المستندات المفتوحة (تسمى أيضاً علامة ذكية علي مستوى التطبيق ).

لإضافة علامة ذكية إلى مستند معين

  1. قم بإنشاء كائن SmartTag، وقم بتكوين هذا الكائن لتعرّف سلوك العلامة الذكية:

    • لتحديد النص الذي تريد أن تعرفه، قم باستخدام الخاصية Terms أو Expressions.

    • لتعريف الإجراءات التى يمكن للمستخدمين النقر فوقها على العلامة الذكية، قم بإضافة واحد أو أكثر من كائن Action إلى الخاصية Actions.

    لمزيد من المعلومات، راجع بنية العلامات الذكية.

  2. إلى ينزّل Microsoft.Office.Tools.Word.Document، راجع GetVstoObjecthttp://انتقال.microsoft.com/fwlink/?LinkId=178958 . لتشغيل الحلول التي تم إنشاؤها باستخدام Microsoft المكتب أدوات المطورين في توسيع مستندات Word ومصنفات Excel في وظائف إضافية على مستوى التطبيق في وقت التشغيل يجب أن يكون مثبتاً تشغيل أجهزة كمبيوتر مستخدم النهائي.

  3. قم بإضافة SmartTag إلى الخاصية VstoSmartTags من Microsoft.Office.Tools.Word.Document.

مثال التعليمات البرمجية التالي يقوم بإنشاء علامة ذكية في المستند النشط تتعرف علي الكلمات term و recognize. عندما ينقر المستخدم فوق العلامة الذكية , فإنه يعرض مواضع أحرف البدء والانتهاء للكلمة التي تم التعرف عليها. لتشغيل هذه التعليمات البرمجية، قم بإضافة التعليمات البرمجية إلى الفئة ThisAddIn، ثم قم باستدعاء الأسلوب AddSmartTagToDocument من معالج الحدث ThisAddIn_Startup وقم بتمرير Microsoft.Office.Interop.Word.Document إلى AddSmartTagToDocument.

ملاحظة

المثال التالي يعمل في مشاريع تستهدف .NET Framework 4. لاستخدام هذا المثال في المشاريع التي تستهدف NET Framework 3.5. راجع التعليقات الموجودة في التعليمات البرمجية.

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTagToDocument(ByVal document As Word.Document)
    ' Create a smart tag for .NET Framework 3.5 projects.
    ' Dim smartTagDemo As New  _
    '    Microsoft.Office.Tools.Word.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")
    ' Create a smart tag for .NET Framework 4 projects.
    Dim  smartTagDemo As SmartTag = Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

    ' Specify the terms to recognize.
    smartTagDemo.Terms.Add("term")
    smartTagDemo.Terms.Add("recognize")

    ' Create the action for .NET Framework 3.5 projects.
    ' displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")
    ' Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() { _
            displayAddress}

    ' Get a Document host item for .NET Framework 3.5
    ' Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
    ' document.GetVstoObject()
    ' Get a Document host item for .NET Framework 4
    Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
        Globals.Factory.GetVstoObject(document)

    ' Add the smart tag to the document.
    vstoDocument.VstoSmartTags.Add(smartTagDemo)
End Sub

Private Sub DisplayAddress_BeforeCaptionShow(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.BeforeCaptionShow

    Dim clickedAction As Microsoft.Office.Tools.Word.Action = _
        TryCast(sender, Microsoft.Office.Tools.Word.Action)

    If clickedAction IsNot Nothing Then
        clickedAction.Caption = "Display the location of " & e.Text
    End If
End Sub

Private Sub DisplayAddress_Click(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.Click

    Dim termStart As Integer = e.Range.Start
    Dim termEnd As Integer = e.Range.End
    MsgBox("The recognized text '" & e.Text & _
            "' begins at position " & termStart & _
            " and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;

private void AddSmartTagToDocument(Word.Document document)
{
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        // Create a smart tag for .NET Framework 3.5 projects.
        //    new Microsoft.Office.Tools.Word.SmartTag(
        //    "www.microsoft.com/Demo#DemoSmartTag",
        //    "Demonstration Smart Tag");
        // Create a smart tag for .NET Framework 4 projects.
        Globals.Factory.CreateSmartTag(
            "www.microsoft.com/Demo#DemoSmartTag",
            "Demonstration Smart Tag");

    // Specify the terms to recognize.
    smartTagDemo.Terms.Add("term");
    smartTagDemo.Terms.Add("recognize");

    // Create the action for .NET Framework 3.5 projects.
    // displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");
    // Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced");

    // Add the action to the smart tag.
    smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { 
        displayAddress };

    // Get a Document host item for .NET Framework 3.5
    // Microsoft.Office.Tools.Word.Document vstoDocument =
    //    document.GetVstoObject();
    // Get a Document host item for .NET Framework 3.5
    Microsoft.Office.Tools.Word.Document vstoDocument =
        Globals.Factory.GetVstoObject(document);
    // Add the smart tag to the document
    vstoDocument.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
        displayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Word.ActionClickEventHandler(
        displayAddress_Click);
}


void displayAddress_BeforeCaptionShow(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    Microsoft.Office.Tools.Word.Action clickedAction =
        sender as Microsoft.Office.Tools.Word.Action;

    if (clickedAction != null)
    {
        clickedAction.Caption = "Display the location of " +
            e.Text;
    }
}

void displayAddress_Click(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    int termStart = e.Range.Start;
    int termEnd = e.Range.End;
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' begins at position " + termStart.ToString() +
        " and ends at position " + termEnd.ToString());
}

لإضافة علامة ذكية تعمل في كافة المستندات المفتوحة

  1. قم بإنشاء كائن SmartTag، وقم بتكوين هذا الكائن لتعرّف سلوك العلامة الذكية:

    • لتحديد النص الذي تريد أن تعرفه، قم باستخدام الخاصية Terms أو Expressions.

    • لتعريف الإجراءات التى يمكن للمستخدمين النقر فوقها على العلامة الذكية، قم بإضافة واحد أو أكثر من كائن Action إلى الخاصية Actions.

    لمزيد من المعلومات، راجع بنية العلامات الذكية.

  2. قم بإضافة SmartTag إلى الخاصية VstoSmartTags من الفئة ThisAddIn.

مثال التعليمة البرمجية التالية يقوم بإنشاء علامة ذكية تتعرف على الكلمات term و recognize. عندما ينقر المستخدم فوق العلامة الذكية , فإنه يعرض مواضع أحرف البدء والانتهاء للكلمة التي تم التعرف عليها. لتشغيل هذه التعليمات البرمجية، قم بإضافة التعليمات البرمجية إلى الفئة ThisAddIn ثم قم باستدعاء الأسلوب AddSmartTag من معالج الحدث ThisAddIn_Startup.

ملاحظة

المثال التالي يعمل في مشاريع تستهدف .NET Framework 4. لاستخدام هذا المثال في المشاريع التي تستهدف NET Framework 3.5. راجع التعليقات الموجودة في التعليمات البرمجية.

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Word.SmartTag = Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

    ' For .NET Framework 3.5 projects, use the following code to create the smart tag.
    ' Dim smartTagDemo As New  _
    '     Microsoft.Office.Tools.Word.SmartTag( _
    '     "www.microsoft.com/Demo#DemoSmartTag", _
    '     "Demonstration Smart Tag")

    ' Specify the terms to recognize.
    smartTagDemo.Terms.Add("term")
    smartTagDemo.Terms.Add("recognize")

    ' Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

    ' For .NET Framework 3.5 projects, use the following code to create the action.
    ' displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() { _
            displayAddress}

    ' Add the smart tag.
    Me.VstoSmartTags.Add(smartTagDemo)
End Sub

Private Sub DisplayAddress_BeforeCaptionShow(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.BeforeCaptionShow

    Dim clickedAction As Microsoft.Office.Tools.Word.Action = _
        TryCast(sender, Microsoft.Office.Tools.Word.Action)

    If clickedAction IsNot Nothing Then
        clickedAction.Caption = "Display the location of " & e.Text
    End If
End Sub

Private Sub DisplayAddress_Click(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.Click

    Dim termStart As Integer = e.Range.Start
    Dim termEnd As Integer = e.Range.End
    MsgBox("The recognized text '" & e.Text & _
            "' begins at position " & termStart & _
            " and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // For .NET Framework 3.5 projects, use the following code to create the smart tag.
    // Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Word.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify the terms to recognize.
    smartTagDemo.Terms.Add("term");
    smartTagDemo.Terms.Add("recognize");

    // Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced"); 

    // For .NET Framework 3.5 projects, use the following code to create the action.
    // displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");

    // Add the action to the smart tag.
    smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { 
        displayAddress };

    // Add the smart tag.
    this.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
        displayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Word.ActionClickEventHandler(
        displayAddress_Click);
}

void displayAddress_BeforeCaptionShow(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    Microsoft.Office.Tools.Word.Action clickedAction =
        sender as Microsoft.Office.Tools.Word.Action;

    if (clickedAction != null)
    {
        clickedAction.Caption = "Display the location of " +
            e.Text;
    }
}

void displayAddress_Click(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    int termStart = e.Range.Start;
    int termEnd = e.Range.End;
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' begins at position " + termStart.ToString() +
        " and ends at position " + termEnd.ToString());
}

أمان

يجب تمكين العلامات الذكية في Word. الوضع الافتراضي أنها غير ممكنة. لمزيد من المعلومات، راجع كيفية القيام بما يلي: تمكين العلامات الذكية في Word وExcel.

راجع أيضًا:

المهام

كيفية القيام بما يلي: تمكين العلامات الذكية في Word وExcel

كيفية القيام بما يلي: إضافة علامات ذكية إلى مصنفات Excel

كيفية القيام بما يلي: إنشاء علامات ذكية بأدوات تعرف مخصصة في Word و .NET Framework 3.5

كيفية القيام بما يلي: إنشاء علامات ذكية بأدوات تعرُّف مخصصة في Excel و .NET Framework 3.5

الإرشادات التفصيلية: إنشاء علامة ذكية باستخدام تخصيص على مستوى المستند

الإرشادات التفصيلية: إنشاء علامة ذكية عن طريق استخدام وظيفة إضافية على مستوى التطبيق

المبادئ

بنية العلامات الذكية

موارد أخرى

نظرة عامة على العلامات الذكية

تطوير حلول Office