مشاركة عبر


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

ينطبق على

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

نوع المشروع

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

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

إصدار Microsoft Office

  • Excel 2007

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

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

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

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

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

ارتباط إلى فيديو للحصول على نسخة فيديو هذا موضوع، راجع هل I: كيف إضافة علامات ذكية إلى مصنفات Excel.

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

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

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

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

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

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

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

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

مثال التعليمات البرمجية التالي يقوم بإنشاء علامة ذكية تتعرف على الكلمة sale والتعبير العادي [I|i]ssue\s\d{5,6}. عندما يكتب المستخدم sale أو سلسلة تطابق التعبير العادي (مثل issue 12345) ثم ينقر على العلامة الذكية، فإنها تعرض موقع خلية النص الذي تم التعرف عليه. لتشغيل هذه التعليمات البرمجية، قم بإضافة التعليمات البرمجية إلى الفئة ThisWorkbook ثم قم باستدعاء الأسلوب AddSmartTag من معالج الحدث ThisWorkbook_Startup.

ملاحظة

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

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Excel.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.Excel.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' 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.Excel.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Excel.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.Excel.ActionEventArgs) _
    Handles DisplayAddress.BeforeCaptionShow

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

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

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.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.Excel.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Excel.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // 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.Excel.Action("To be replaced");

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

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

    displayAddress.BeforeCaptionShow += new 
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new 
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

إضافة علامات ذكية في وظائف إضافية على مستوى التطبيق

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

لإضافة علامة ذكية إلى مصنف معين

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

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

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

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

  2. To create a Microsoft.Office.Tools.Excel.Workbook host item for the workbook that you want to host the smart tag, use the GetVstoObject method. لتشغيل الحلول التي تم إنشاؤها باستخدام Microsoft المكتب أدوات المطورين في توسيع مستندات Word ومصنفات Excel في وظائف إضافية على مستوى التطبيق في وقت التشغيل يجب أن يكون مثبتاً تشغيل أجهزة كمبيوتر مستخدم النهائي.

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

مثال التعليمات البرمجية التالي يقوم بإنشاء علامة ذكية تتعرف على الكلمة sale والتعبير العادي [I|i]ssue\s\d{5,6}. عندما يكتب المستخدم sale أو سلسلة تطابق التعبير العادي (مثل issue 12345) ثم ينقر على العلامة الذكية، فإنها تعرض موقع خلية النص الذي تم التعرف عليه. لتشغيل هذه التعليمات البرمجية، قم بإضافة التعليمات البرمجية إلى الفئة ThisAddIn، ثم قم باستدعاء الأسلوب AddSmartTagToWorkbook من معالج الحدث ThisAddIn_Startup وقم بتمرير Microsoft.Office.Interop.Excel.Workbook إلى AddSmartTagToWorkbook.

ملاحظة

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

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTagToWorkbook(ByVal workbook As Excel.Workbook)
    ' Create a smart tag for .NET Framework 3.5 projects.
    ' Dim smartTagDemo As New  _
    '    Microsoft.Office.Tools.Excel.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 a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' Create the action for .NET Framework 3.5 projects.
    ' displayAddress = New Microsoft.Office.Tools.Excel.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.Excel.Action() { _
            displayAddress}


    ' Get the host item for the workbook in .NET Framework 3.5 projects.
    ' Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook = _
    '  workbook.GetVstoObject()
    ' Get the host item for the workbook in .NET Framework 4 projects.
    Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook = _
        Globals.Factory.GetVstoObject(workbook)

    ' Add the smart tag to the active workbook.
    vstoWorkbook.VstoSmartTags.Add(smartTagDemo)
End Sub


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

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

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

End Sub

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTagToWorkbook(Excel.Workbook workbook)
{
    Microsoft.Office.Tools.Excel.SmartTag smartTagDemo =
        // Create a smart tag for .NET Framework 3.5 projects.
        // new Microsoft.Office.Tools.Excel.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 a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // Create the action for .NET Framework 3.5 projects.
    // displayAddress = new Microsoft.Office.Tools.Excel.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.Excel.Action[] { displayAddress };

    // Get the host item for the workbook in .NET Framework 3.5 projects.
    // Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
    //    workbook.GetVstoObject();
    // Get the host item for the workbook in .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
        Globals.Factory.GetVstoObject(workbook);

    // Add the smart tag to the active workbook.
    vstoWorkbook.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender,
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

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

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

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

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

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

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

مثال التعليمات البرمجية التالي يقوم بإنشاء علامة ذكية تتعرف على الكلمة sale والتعبير العادي [I|i]ssue\s\d{5,6}. عندما يكتب المستخدم sale أو سلسلة تطابق التعبير العادي (مثل issue 12345) ثم ينقر على العلامة الذكية، فإنها تعرض موقع خلية النص الذي تم التعرف عليه. لتشغيل هذه التعليمات البرمجية، قم بإضافة التعليمات البرمجية إلى الفئة ThisAddIn ثم قم باستدعاء الأسلوب AddSmartTag من معالج الحدث ThisAddIn_Startup.

ملاحظة

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

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Excel.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.Excel.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' 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.Excel.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Excel.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.Excel.ActionEventArgs) _
    Handles DisplayAddress.BeforeCaptionShow

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

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

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.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.Excel.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Excel.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // 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.Excel.Action("To be replaced");

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

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

    displayAddress.BeforeCaptionShow += new 
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new 
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

أمان

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

راجع أيضًا:

المهام

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

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

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

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

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

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

المبادئ

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

موارد أخرى

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

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

تطوير حلول Office