방법: Excel 및 .NET Framework 3.5에서 사용자 지정 인식기를 사용하여 스마트 태그 만들기

.NET Framework 3.5를 대상으로 하는 Excel 프로젝트에서는 Microsoft.Office.Tools.Excel.SmartTag 클래스의 파생 클래스를 만들고 Recognize 메서드를 재정의하여 Excel에서 문서의 스마트 태그가 인식되는 방식을 제어할 수 있습니다.

스마트 태그를 실행하려면 최종 사용자가 스마트 태그를 사용하도록 설정되어 있어야 합니다. 자세한 내용은 방법: Word 및 Excel에서 스마트 태그 사용을 참조하십시오.

적용 대상: 이 항목의 정보는 Excel 2007의 문서 수준 프로젝트 및 응용 프로그램 수준 프로젝트에 적용됩니다. 자세한 내용은 Office 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

사용자 지정 인식자로 스마트 태그를 Excel 통합 문서에 추가하려면

  1. Excel 2007용 문서 수준 또는 응용 프로그램 수준 프로젝트를 새로 만듭니다. 자세한 내용은 방법: Visual Studio에서 Office 프로젝트 만들기를 참조하십시오.

  2. 참조 추가 대화 상자의 .NET 탭에서 Microsoft.Office.Interop.SmartTag 어셈블리(버전 12.0.0.0)에 대한 참조를 추가합니다.

  3. 프로젝트에 클래스 파일을 추가하고 Microsoft.Office.Tools.Excel.SmartTag에서 상속한 클래스를 만듭니다.

  4. 새 클래스에서 스마트 태그에 대한 작업을 만듭니다. 작업은 스마트 태그 메뉴에 나타나는 항목입니다. Action 형식의 인스턴스를 클래스의 Actions 컬렉션에 추가하여 작업을 만듭니다.

  5. SmartTagBase.Recognize 메서드를 재정의하여 사용자 고유의 사용자 지정 인식 동작을 구현합니다.

    Recognize 메서드를 재정의할 때는 Microsoft.Office.Tools.Excel.SmartTag.PersistTag 메서드를 호출하여 Excel에서 스마트 태그를 인식할 수 있게 해야 합니다.

  6. 앞서 만든 작업의 Click 이벤트와 필요할 경우 BeforeCaptionShow 이벤트에 응답하는 이벤트 처리기를 만듭니다.

  7. 프로젝트 통합 문서의 코드 파일에서 ThisWorkbook 클래스의 VstoSmartTags 속성(문서 수준 프로젝트의 경우) 또는 ThisAddIn 클래스의 VstoSmartTags 속성(응용 프로그램 수준 프로젝트의 경우)에 스마트 태그 인스턴스를 추가합니다.

예제

다음 코드 예제에서는 Excel 통합 문서에 사용자 지정 스마트 태그를 만드는 방법을 보여 줍니다. 이 예제에서는 sales와 organization이라는 용어를 인식하도록 Recognize 메서드를 재정의합니다. Recognize 메서드는 스마트 태그의 키 속성 컬렉션에 키-값 쌍을 추가한 다음 PersistTag 메서드를 호출하여 스마트 태그를 인식하고 새 스마트 태그 속성을 저장합니다.

예제를 테스트하려면 sales와 organization이라는 단어를 통합 문서의 각각 다른 셀에 입력한 다음 스마트 태그 작업을 시도합니다. 한 작업을 수행하면 인식된 단어에 해당하는 속성 값이 표시되고 또 다른 작업을 수행하면 스마트 태그 네임스페이스 및 캡션이 표시됩니다.

Imports Microsoft.Office.Tools.Excel
Imports Microsoft.Office.Interop.SmartTag

Public Class CustomSmartTag
    Inherits SmartTag

    ' Declare Actions for this SmartTag
    WithEvents Action1 As New Action("Display property value")
    WithEvents Action2 As New Action("Display smart tag details")

    Public Sub New()
        MyBase.New("https://www.contoso.com/Demo#DemoSmartTag", _
            "Custom Smart Tag")
        Me.Terms.AddRange(New String() {"sales", "organization"})
        Actions = New Action() {Action1, Action2}
    End Sub

    Protected Overrides Sub Recognize(ByVal text As String, _
        ByVal site As ISmartTagRecognizerSite, _
        ByVal tokenList As ISmartTagTokenList)

        ' Determine whether each smart tag term exists in 
        ' the document text.
        Dim Term As String
        For Each Term In Me.Terms

            ' Search the cell text for the first instance of 
            ' the current smart tag term.
            Dim index As Integer = Me.CellText.IndexOf(Term, 0)

            If (index >= 0) Then

                ' 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)

                ' Attach the smart tag to the term in the document
                Me.PersistTag(propertyBag)

                ' This implementation only finds the first instance
                ' of a smart tag term in the cell. 
                Exit For
            End If
        Next
    End Sub

    ' This action displays the property value for the term.
    Private Sub Action1_Click(ByVal sender As Object, _
        ByVal e As ActionEventArgs) Handles Action1.Click

        Dim propertyBag As ISmartTagProperties = e.Properties
        Dim key As String = "Key1"
        MsgBox("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 ActionEventArgs) Handles Action2.Click

        MsgBox("The current smart tag caption is '" & _
            Me.Caption & "'. The current smart tag type is '" & _
            Me.SmartTagType & "'.")
    End Sub
End Class
using System;
using System.Windows.Forms;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Interop.SmartTag;

    public class CustomSmartTag : SmartTag {

        // Declare Actions for this SmartTag
        Microsoft.Office.Tools.Excel.Action Action1 =
            new Microsoft.Office.Tools.Excel.Action("Display property value");
        Microsoft.Office.Tools.Excel.Action Action2 =
            new Microsoft.Office.Tools.Excel.Action("Display smart tag details");

        public CustomSmartTag() : base(
            "https://www.contoso.com/Demo#DemoSmartTag", 
            "Custom Smart Tag")
        {
            this.Terms.AddRange(new string[] { 
                "sales", "organization" });
            Actions = new Microsoft.Office.Tools.Excel.Action[] { Action1, Action2 };
            Action1.Click +=
                new ActionClickEventHandler(Action1_Click);
            Action2.Click += 
                new ActionClickEventHandler(Action2_Click);
        }

        protected override void Recognize(string text, 
            ISmartTagRecognizerSite site, ISmartTagTokenList tokenList)
        {
            // Determine whether each smart tag term exists in 
            // the document text.
            foreach (string term in this.Terms)
            {
                // Search the cell text for the first instance of 
                // the current smart tag term.
                int index = this.CellText.IndexOf(term, 0);

                if (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
                    this.PersistTag(propertyBag);

                    // This implementation only finds the first instance
                    // of a smart tag term in the cell. 
                    break;
                }
            }
        }

        // This action displays the property value for the term.
        private void Action1_Click(object sender, 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, ActionEventArgs e)
        {
            MessageBox.Show("The current smart tag caption is '" + 
                this.Caption + "'. The current smart tag type is '" + 
                this.SmartTagType + "'.");
        }
    }

코드 컴파일

  • 참조 추가 대화 상자의 COM 탭에서 Microsoft Smart Tags 2.0 Type Library에 대한 참조를 프로젝트에 추가합니다. 참조의 로컬 복사 속성이 false인지 확인합니다. 이 속성이 true이면 올바른 주 Interop 어셈블리가 참조되지 않으므로 Microsoft Office 설치 미디어를 사용하여 어셈블리를 설치해야 합니다. 자세한 내용은 방법: Office 주 Interop 어셈블리 설치를 참조하십시오.

  • 예제 코드를 CustomSmartTag라는 새 클래스 파일에 저장합니다.

  • C#의 경우 프로젝트 이름과 일치하도록 네임스페이스를 변경합니다.

  • 클래스 파일의 맨 위에 Microsoft.Office.Tools.ExcelMicrosoft.Office.Interop.SmartTag 네임스페이스에 대한 Imports(Visual Basic의 경우) 또는 using(C#의 경우) 문을 추가합니다.

  • 프로젝트의 ThisWorkbook_Startup 또는 ThisAddIn_Startup 이벤트 처리기에 다음 코드를 추가합니다. 이 코드는 통합 문서에 사용자 지정 스마트 태그를 추가합니다.

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

보안

Excel에서 스마트 태그를 사용하도록 설정해야 합니다. 기본적으로 Excel에서는 스마트 태그를 사용하도록 설정되어 있지 않습니다. 자세한 내용은 방법: Word 및 Excel에서 스마트 태그 사용을 참조하십시오.

참고 항목

작업

방법: Word 및 Excel에서 스마트 태그 사용

방법: Word 문서에 스마트 태그 추가

방법: Excel 통합 문서에 스마트 태그 추가

방법: Word 및 .NET Framework 3.5에서 사용자 지정 인식기를 사용하여 스마트 태그 만들기

연습: 문서 수준 사용자 지정을 사용하여 스마트 태그 만들기

연습: 응용 프로그램 수준 추가 기능을 사용하여 스마트 태그 만들기

개념

스마트 태그 아키텍처

기타 리소스

스마트 태그 개요

Office UI 사용자 지정