مشاركة عبر


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

ينطبق على

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

نوع المشروع

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

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

إصدار Microsoft Office

  • Word 2007

في مشاريع Word التي تستهدف .NET Framework 4 ، يمكنك التحكم في كيفية تعرف Word على العلامات الذكية في المستندات عن طريق تطبيق الواجهة ISmartTagExtension.

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

لإضافة علامة ذكية بأداة تعرٌّف مخصصة إلى مستند Word

  1. للوصول إلى الأعضاء التي تشترك فيها عناصر التحكم، استخدم ، ، أو كائن. لمزيد من المعلومات، راجع كيفية القيام بما يلي: إنشاء مشاريع Office في Visual Studio.

  2. قم بإضافة مرجع إلى تجميع Microsoft.المكتب.Interop.SmartTag (الإصدار 12.0.0.0) من علامة تبويب .NET إضافة مرجع الخاصة صندوق حوار.

  3. قم بإضافة ملف فئة إلى المشروع وقم بإنشاء فئة تطبق الواجهة ISmartTagExtension.

  4. في الفئة الجديدة ، قم بإنشاء كائن SmartTag الذي يمثل العلامة الذكية وإنشاء كائن Action واحد أو أكثر يمثل إجراءات العلامة الذكية. استخدم أساليبGlobals.Factory.CreateSmartTag و Globals.Factory.CreateAction لإنشاء هذه الكائنات.

  5. طبق الأسلوب Recognize و اكتب سلوك التعرف المخصص الخاص بك .

    يجب أن يستدعي تطبيقك الأسلوب PersistTag للمعلمة context لجعل Word يتعرف على العلامة الذكية.

  6. قم بتنفيذ خاصية ExtensionBase لإرجاع الكائن SmartTag .

  7. قم بإنشاء معالجات الأحداث للاستجابة للحدث Click ، واختيارياً الحدث BeforeCaptionShow من الإجراءات التي قمت بإنشائها.

  8. في ملف التعليمات البرمجية في مستند المشروع، قم بإضافة مثيل علامة ذكية إلى الخاصية VstoSmartTags من الفئة ThisDocument (لمشروع على مستوى المستند)، أو الخاصية VstoSmartTags من الفئة ThisAddIn (لمشروع على مستوى التطبيق).

مثال

يظهر مثال التعليمات البرمجية التالي كيفية إنشاء علامة ذكية مخصصة في مستند Word. المثال يطبق أسلوب Recognize للتعرف على المصطلحات sales و organization. الأسلوب Recognize يقوم بإضافة زوج من قيمة و مفتاح إلى مجموعة الخصائص ذات المفاتيح للعلامة الذكية. ثم يقوم الأسلوب باستدعاء أسلوب PersistTag للتعرف على العلامات الذكية وحفظ خصائص العلامات الذكية الجديدة .

لاختبار المثال اكتب الكلمات sales(المبيعات) و organization (المؤسسة) في أماكن مختلفة في المستند وجرب بعدها إجراءات العلامات الذكية. أحد الإجراءات يعرض قيمة الخاصية المطابقة للمصطلح المعرف و إجراء آخر يعرض مساحة اسم العلامة الذكية و التسمية التوضيحية.

Imports System
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.SmartTag
Imports Microsoft.Office.Tools.Word

Public Class CustomSmartTag
    Implements ISmartTagExtension
    ' Declare the smart tag.
    Private smartTagDemo As Microsoft.Office.Tools.Word.SmartTag

    ' Declare actions for this smart tag.
    WithEvents Action1 As Microsoft.Office.Tools.Word.Action
    WithEvents Action2 As Microsoft.Office.Tools.Word.Action

    Public Sub New()
        Me.smartTagDemo = Globals.Factory.CreateSmartTag(
            "https://www.contoso.com/Demo#DemoSmartTag", "Custom Smart Tag", Me)

        Action1 = Globals.Factory.CreateAction("Display property value")
        Action2 = Globals.Factory.CreateAction("Display smart tag details")

        smartTagDemo.Terms.AddRange(New String() {"sales", "organization"})
        smartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() {Action1, Action2}

    End Sub

    Private Sub Recognize(ByVal text As String,
        ByVal site As ISmartTagRecognizerSite,
        ByVal tokenList As ISmartTagTokenList,
        ByVal context As SmartTagRecognizeContext) Implements ISmartTagExtension.Recognize

        For Each term As String In smartTagDemo.Terms
            ' Search the text for the current smart tag term.
            Dim index As Integer = text.IndexOf(term, 0)

            While (index >= 0)
                ' Create a smart tag token and a property bag for the recognized term.
                Dim propertyBag As ISmartTagProperties = site.GetNewPropertyBag()

                ' Write a new property value.
                Dim key As String = "Key1"
                propertyBag.Write(key, DateTime.Now.ToString())

                ' Attach the smart tag to the term in the document
                context.PersistTag(index, term.Length, propertyBag)

                ' Increment the index and then find the next instance of the smart tag term.
                index += term.Length
                index = text.IndexOf(term, index)
            End While
        Next
    End Sub

    ' This action displays the property value for the term.
    Private Sub Action1_Click(ByVal sender As Object,
        ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) Handles Action1.Click
        Dim propertyBag As ISmartTagProperties = e.Properties
        Dim key As String = "Key1"
        MessageBox.Show(("The corresponding value of " & key & " is: ") + propertyBag.Read(key))
    End Sub

    ' This action displays smart tag details.
    Private Sub Action2_Click(ByVal sender As Object,
        ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) Handles Action2.Click
        MessageBox.Show(("The current smart tag caption is '" &
            smartTagDemo.Caption & "'. The current smart tag type is '") &
            smartTagDemo.SmartTagType & "'.")
    End Sub

    Public ReadOnly Property Base() As Microsoft.Office.Tools.Word.SmartTag
        Get
            Return (smartTagDemo)
        End Get
    End Property

    Public ReadOnly Property ExtensionBase() As Object Implements ISmartTagExtension.ExtensionBase
        Get
            Return (smartTagDemo)
        End Get
    End Property
End Class
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.SmartTag;
using Microsoft.Office.Tools.Word;

namespace CustomSmartTagExample
{
    public class CustomSmartTag : ISmartTagExtension
    {
        // Declare the smart tag.
        Microsoft.Office.Tools.Word.SmartTag smartTagDemo;

        // Declare actions for this smart tag.
        private Microsoft.Office.Tools.Word.Action Action1;
        private Microsoft.Office.Tools.Word.Action Action2;

        public CustomSmartTag()
        {
            this.smartTagDemo = Globals.Factory.CreateSmartTag(
                "https://www.contoso.com/Demo#DemoSmartTag", "Custom Smart Tag", this);

            Action1 = Globals.Factory.CreateAction("Display property value");
            Action2 = Globals.Factory.CreateAction("Display smart tag details");

            smartTagDemo.Terms.AddRange(new string[] { "sales", "organization" });
            smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { Action1, Action2 };

            Action1.Click += new ActionClickEventHandler(Action1_Click);
            Action2.Click += new ActionClickEventHandler(Action2_Click);
        }

        void ISmartTagExtension.Recognize(string text, ISmartTagRecognizerSite site, ISmartTagTokenList tokenList, 
            SmartTagRecognizeContext context)
        {

            foreach (string term in smartTagDemo.Terms)
            {
                // Search the text for the current smart tag term.
                int index = text.IndexOf(term, 0);

                while (index >= 0)
                {
                    // Create a smart tag token and a property bag for the recognized term.
                    ISmartTagProperties propertyBag = site.GetNewPropertyBag();

                    // Write a new property value.
                    string key = "Key1";
                    propertyBag.Write(key, DateTime.Now.ToString());

                    // Attach the smart tag to the term in the document
                    context.PersistTag(index, term.Length, propertyBag);

                    // Increment the index and then find the next instance of the smart tag term.
                    index += term.Length;
                    index = text.IndexOf(term, index);
                }
            }
        }

        // This action displays the property value for the term.
        private void Action1_Click(object sender,
            Microsoft.Office.Tools.Word.ActionEventArgs e)
        {
            ISmartTagProperties propertyBag = e.Properties;
            string key = "Key1";
            MessageBox.Show("The corresponding value of " + key + " is: " + propertyBag.get_Read(key));
        }

        // This action displays smart tag details.
        private void Action2_Click(object sender,
            Microsoft.Office.Tools.Word.ActionEventArgs e)
        {
            MessageBox.Show("The current smart tag caption is '" +
                smartTagDemo.Caption + "'. The current smart tag type is '" + smartTagDemo.SmartTagType + "'.");
        }


        public Microsoft.Office.Tools.Word.SmartTag Base
        {
            get { return smartTagDemo; }
        }

        public object ExtensionBase
        {
            get { return smartTagDemo; }
        }

    }
}

التحويل البرمجي للتعليمات البرمجية

  • قم بإضافة مرجع في المشروع إلى Microsoft Smart Tags 2.0 Type Library من علامة التبويب COM فى مربع الحوار إضافة مرجع . تأكد من إن خاصية النسخ المحلي (Copy local) للمرجع قيمتها false. إذا كانت true ، إذاً فالمرجع لا يشير إلى تجميع التوافق الأساسي الصحيح ويجب عليك أن تقوم بتثبيت التجميع من وسائط تثبيت Microsoft Office . لمزيد من المعلومات، راجع كيفية القيام بما يلي: تثبيت تجميعات توافق Office أساسية.

  • ضع مثال التعليمات البرمجية في ملف فئة جديد باسم CustomSmartTag.

  • في C# ، قم بتغيير مساحة الاسم لتطابق اسم المشروع.

  • قم بإضافة عبارات Imports (في Visual Basic) أو using (في C#) لمساحات الأسماء Microsoft.Office.Tools.Word و Microsoft.Office.Interop.SmartTag في أعلى ملف الفئة.

  • قم بإضافة التعليمات البرمجية التالية إلى معالج الحدث ThisDocument_Startup أو ThisAddIn_Startup في المشروع الخاص بك. تعمل هذه التعليمة البرمجية على إضافة علامة ذكية مخصصة إلى المستند .

    Me.VstoSmartTags.Add(New CustomSmartTag().Base)
    
    this.VstoSmartTags.Add(new CustomSmartTag().Base);
    

أمان

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

راجع أيضًا:

المهام

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

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

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

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

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

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

المبادئ

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

موارد أخرى

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