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

업데이트: 2008년 7월

적용 대상

이 항목의 정보는 지정된 Visual Studio Tools for Office 프로젝트 및 Microsoft Office 버전에만 적용됩니다.

문서 수준 프로젝트

  • Word 2003

  • Word 2007

응용 프로그램 수준 프로젝트

  • Word 2007

자세한 내용은 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

Microsoft Office Word 문서에 스마트 태그를 추가하여 텍스트를 인식하고 인식된 용어와 관련된 동작에 사용자가 액세스하도록 할 수 있습니다.

Visual Studio 2008 SP 1(서비스 팩 1)부터는 응용 프로그램 수준 추가 기능을 사용하여 열려 있는 문서에 스마트 태그를 추가할 수 있습니다. 스마트 태그를 만들고 구성하기 위해 작성하는 코드는 문서 수준 프로젝트의 경우와 응용 프로그램 수준 프로젝트의 경우에 모두 동일하지만 스마트 태그를 문서와 연결하는 방식에는 몇 가지 차이점이 있습니다. 또한 스마트 태그의 범위도 문서 수준 프로젝트와 응용 프로그램 수준 프로젝트에서 서로 다릅니다.

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

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

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

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

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

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

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

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

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

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

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

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

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

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTag()
    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.
    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 OpenMessageBox_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()
{
    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.
    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());
}

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

SP1부터는 응용 프로그램 수준 추가 기능을 사용하여 스마트 태그를 추가할 수 있습니다. 스마트 태그가 특정 문서에서만 작동하도록 할지 열려 있는 모든 문서에서 작동하도록 할지(응용 프로그램 수준 스마트 태그라고도 함)를 지정할 수 있습니다.

특정 문서에 스마트 태그를 추가하려면

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

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

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

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

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

    참고:

    SP1을 설치하기 전에 만든 프로젝트를 사용하는 경우 GetVstoObject 메서드를 사용하려면 먼저 프로젝트를 수정해야 합니다. 자세한 내용은 런타임에 응용 프로그램 수준 추가 기능의 Word 문서 및 Excel 통합 문서 확장을 참조하십시오.

  3. DocumentVstoSmartTags 속성에 SmartTag를 추가합니다.

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

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTagToActiveDocument()
    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.
    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}

    ' Get a Document host item, and add the smart tag to the document.
    Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
        Me.Application.ActiveDocument.GetVstoObject()
    vstoDocument.VstoSmartTags.Add(smartTagDemo)
End Sub

Private Sub OpenMessageBox_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 AddSmartTagToActiveDocument()
{
    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.
    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 to the document.
    Microsoft.Office.Tools.Word.Document vstoDocument =
        this.Application.ActiveDocument.GetVstoObject();
    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를 추가합니다.

    참고:

    SP1을 설치하기 전에 만든 프로젝트를 사용하는 경우 VstoSmartTags 속성을 생성하려면 프로젝트를 수정해야 합니다. 자세한 내용은 방법: 응용 프로그램 수준 스마트 태그를 SP1 이전에 만든 프로젝트에 추가을를 참조하십시오.

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

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

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

참고 항목

작업

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

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

방법: 응용 프로그램 수준 스마트 태그를 SP1 이전에 만든 프로젝트에 추가

방법: Word에서 사용자 지정 인식자로 스마트 태그 만들기

방법: Excel에서 사용자 지정 인식자로 스마트 태그 만들기

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

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

개념

스마트 태그 개요

스마트 태그 아키텍처

스마트 태그 아키텍처

Office 솔루션 개발

변경 기록

날짜

변경 내용

원인

2008년 7월

응용 프로그램 수준 추가 기능에 대한 절차가 새로 추가되었습니다.

SP1 기능 변경