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

Microsoft Office Excel 통합 문서에 스마트 태그를 추가하여 텍스트를 인식하고 인식된 용어와 관련된 동작에 사용자가 액세스하도록 할 수 있습니다. 스마트 태그를 만들고 구성하기 위해 작성하는 코드는 문서 수준 프로젝트의 경우와 응용 프로그램 수준 프로젝트의 경우에 모두 동일하지만 스마트 태그를 통합 문서와 연결하는 방식에는 몇 가지 차이점이 있습니다. 또한 스마트 태그의 범위도 문서 수준 프로젝트와 응용 프로그램 수준 프로젝트에서 서로 다릅니다.

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

이 항목에서는 다음 작업에 대해 설명합니다.

  • 문서 수준 사용자 지정에서 스마트 태그 추가

  • 응용 프로그램 수준 추가 기능에서 스마트 태그 추가

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

비디오에 링크 이 항목의 비디오 버전을 보려면 How Do I: Add Smart Tags to Excel Workbooks를 참조하십시오.

문서 수준 사용자 지정에서 스마트 태그 추가

문서 수준 사용자 지정을 사용하여 스마트 태그를 추가할 경우 스마트 태그는 해당 사용자 지정과 연결된 통합 문서에서만 인식됩니다.

문서 수준 사용자 지정을 사용하여 스마트 태그를 추가하려면

  1. SmartTag 개체를 만들고 스마트 태그의 동작을 정의하도록 구성합니다.

    • 인식할 텍스트를 지정하려면 Terms 또는 Expressions 속성을 사용합니다.

    • 사용자가 스마트 태그에 대해 클릭하여 실행할 수 있는 작업을 정의하려면 Actions 속성에 Action 개체를 하나 이상 추가합니다.

    자세한 내용은 스마트 태그 아키텍처를 참조하십시오.

  2. ThisWorkbook 클래스의 VstoSmartTags 속성에 SmartTag를 추가합니다.

다음 코드 예제에서는 sale이라는 단어와 [I|i]ssue\s\d{5,6} 정규식을 인식하는 스마트 태그를 만듭니다. 사용자가 sale 또는 정규식에 일치하는 문자열(예: issue 12345)을 입력한 다음 스마트 태그를 클릭하면 인식된 텍스트의 셀 위치가 표시됩니다. 이 코드를 실행하려면 ThisWorkbook 클래스에 코드를 추가하고 ThisWorkbook_Startup 이벤트 처리기에서 AddSmartTag 메서드를 호출합니다.

참고

다음 예제에서는 .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 속성을 사용합니다.

    • 사용자가 스마트 태그에 대해 클릭하여 실행할 수 있는 작업을 정의하려면 Actions 속성에 Action 개체를 하나 이상 추가합니다.

    자세한 내용은 스마트 태그 아키텍처를 참조하십시오.

  2. 스마트 태그를 호스팅할 통합 문서에 대한 Microsoft.Office.Tools.Excel.Workbook 호스트 항목을 만들려면 GetVstoObject 메서드를 사용합니다. 호스트 항목을 만드는 방법에 대한 자세한 내용은 런타임에 응용 프로그램 수준 추가 기능의 Word 문서 및 Excel 통합 문서 확장을 참조하십시오.

  3. Microsoft.Office.Tools.Excel.WorkbookVstoSmartTags 속성에 SmartTag를 추가합니다.

다음 코드 예제에서는 sale이라는 단어와 [I|i]ssue\s\d{5,6} 정규식을 인식하는 스마트 태그를 만듭니다. 사용자가 sale 또는 정규식에 일치하는 문자열(예: issue 12345)을 입력한 다음 스마트 태그를 클릭하면 인식된 텍스트의 셀 위치가 표시됩니다. 이 코드를 실행하려면 ThisAddIn 클래스에 코드를 추가하고, ThisAddIn_Startup 이벤트 처리기에서 AddSmartTagToWorkbook 메서드를 호출하고, AddSmartTagToWorkbook에 Microsoft.Office.Interop.Excel.Workbook을 전달합니다.

참고

다음 예제에서는 .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 속성을 사용합니다.

    • 사용자가 스마트 태그에 대해 클릭하여 실행할 수 있는 작업을 정의하려면 Actions 속성에 Action 개체를 하나 이상 추가합니다.

    자세한 내용은 스마트 태그 아키텍처를 참조하십시오.

  2. ThisAddIn 클래스의 VstoSmartTags 속성에 SmartTag를 추가합니다.

다음 코드 예제에서는 sale이라는 단어와 [I|i]ssue\s\d{5,6} 정규식을 인식하는 스마트 태그를 만듭니다. 사용자가 sale 또는 정규식에 일치하는 문자열(예: issue 12345)을 입력한 다음 스마트 태그를 클릭하면 인식된 텍스트의 셀 위치가 표시됩니다. 이 코드를 실행하려면 ThisAddIn 클래스에 코드를 추가하고 ThisAddIn_Startup 이벤트 처리기에서 AddSmartTag 메서드를 호출합니다.

참고

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

참고 항목

작업

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

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

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

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

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

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

개념

스마트 태그 아키텍처

기타 리소스

How Do I: Add Smart Tags to Excel Workbooks?

스마트 태그 개요

Office 솔루션 개발