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

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

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

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

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

  • 응용 프로그램 수준 추가 기능을 사용하여 스마트 태그 추가

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

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

문서 수준 사용자 지정의 스마트 태그는 해당 사용자 지정과 연결된 문서에서만 인식됩니다.

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

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

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

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

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

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

다음 코드 예제에서는 term 및 recognize라는 단어를 인식하는 스마트 태그를 만듭니다. 사용자가 스마트 태그를 클릭하면 인식된 단어의 시작 문자 및 끝 문자 위치가 표시됩니다. 이 코드를 실행하려면 ThisDocument 클래스에 코드를 추가하고 ThisDocument_Startup 이벤트 처리기에서 AddSmartTag 메서드를 호출합니다.

참고

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

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

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

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

  3. Microsoft.Office.Tools.Word.DocumentVstoSmartTags 속성에 SmartTag를 추가합니다.

다음 코드 예제에서는 활성 문서에 term 및 recognize라는 단어를 인식하는 스마트 태그를 만듭니다. 사용자가 스마트 태그를 클릭하면 인식된 단어의 시작 문자 및 끝 문자 위치가 표시됩니다. 이 코드를 실행하려면 ThisAddIn 클래스에 코드를 추가하고, ThisAddIn_Startup 이벤트 처리기에서 AddSmartTagToDocument 메서드를 호출하고, AddSmartTagToDocument에 Microsoft.Office.Interop.Word.Document를 전달합니다.

참고

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

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

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

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

다음 코드 예제에서는 term 및 recognize라는 단어를 인식하는 스마트 태그를 만듭니다. 사용자가 스마트 태그를 클릭하면 인식된 단어의 시작 문자 및 끝 문자 위치가 표시됩니다. 이 코드를 실행하려면 ThisAddIn 클래스에 코드를 추가하고 ThisAddIn_Startup 이벤트 처리기에서 AddSmartTag 메서드를 호출합니다.

참고

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

참고 항목

작업

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

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

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

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

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

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

개념

스마트 태그 아키텍처

기타 리소스

스마트 태그 개요

Office 솔루션 개발