방법: Word 문서에 콘텐츠 컨트롤 추가

문서 수준 Word 프로젝트에서는 디자인 타임이나 런타임에 프로젝트의 문서에 콘텐츠 컨트롤을 추가할 수 있습니다. 응용 프로그램 수준 Word 프로젝트에서는 런타임에 열려 있는 문서에 콘텐츠 컨트롤을 추가할 수 있습니다.

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

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

  • 디자인 타임에 콘텐츠 컨트롤 추가

  • 런타임에 문서 수준 프로젝트에 콘텐츠 컨트롤 추가

  • 런타임에 응용 프로그램 수준 프로젝트에 콘텐츠 컨트롤 추가

콘텐츠 컨트롤에 대한 자세한 내용은 콘텐츠 컨트롤을 참조하십시오.

디자인 타임에 콘텐츠 컨트롤 추가

디자인 타임에 다음과 같은 방법으로 문서 수준 프로젝트의 문서에 콘텐츠 컨트롤을 추가할 수 있습니다.

  • 도구 상자Word 컨트롤 탭에서 콘텐츠 컨트롤을 추가합니다.

  • Word에서 네이티브 콘텐츠 컨트롤을 추가할 때와 동일한 방식으로 문서에 콘텐츠 컨트롤을 추가합니다.

  • 데이터 소스 창에서 콘텐츠 컨트롤을 문서로 끌어 옵니다. 이 방법은 컨트롤이 만들어질 때 해당 컨트롤을 데이터에 바인딩하려는 경우에 유용합니다. 자세한 내용은 방법: 개체의 데이터로 문서 채우기방법: 데이터베이스의 데이터로 문서 채우기을 참조하십시오.

참고

다음 지침처럼 컴퓨터에서 Visual Studio 사용자 인터페이스 요소 일부에 대한 이름이나 위치를 다르게 표시할 수 있습니다. 이러한 요소는 사용하는 Visual Studio 버전 및 설정에 따라 결정됩니다. 자세한 내용은 Visual Studio 설정을 참조하십시오.

도구 상자를 사용하여 문서에 콘텐츠 컨트롤을 추가하려면

  1. Visual Studio 디자이너에 호스팅된 문서에서 콘텐츠 컨트롤을 추가할 위치에 커서를 놓거나 콘텐츠 컨트롤로 대체할 텍스트를 선택합니다.

  2. 도구 상자를 열고 Word 컨트롤 탭을 클릭합니다.

  3. 다음 중 한 가지 방법으로 컨트롤을 추가합니다.

    • 도구 상자의 콘텐츠 컨트롤을 두 번 클릭합니다.

      또는

    • 도구 상자의 콘텐츠 컨트롤을 클릭하고 Enter 키를 누릅니다.

      또는

    • 콘텐츠 컨트롤을 도구 상자에서 문서로 끌어 옵니다. 그러면 마우스 포인터 위치가 아니라 문서의 현재 선택 영역에 콘텐츠 컨트롤이 추가됩니다.

참고

GroupContentControl도구 상자를 사용하여 추가할 수 없습니다. Word에서 또는 런타임에만 GroupContentControl를 추가할 수 있습니다.

참고

Visual Studio는 Word 2010 프로젝트를 위해 도구 상자에 확인란 콘텐츠 컨트롤을 제공하지 않습니다. 문서에 확인란 콘텐츠 컨트롤을 추가하려면 ContentControl 개체를 프로그래밍 방식으로 만들어야 합니다. 자세한 내용은 콘텐츠 컨트롤을 참조하십시오.

Word에서 문서에 콘텐츠 컨트롤을 추가하려면

  1. Visual Studio 디자이너에 호스팅된 문서에서 콘텐츠 컨트롤을 추가할 위치에 커서를 놓거나 콘텐츠 컨트롤로 대체할 텍스트를 선택합니다.

  2. 리본 메뉴에서 개발 도구 탭을 클릭합니다.

    참고

    개발 도구 탭이 표시되지 않으면 먼저 이 탭을 표시해야 합니다. 자세한 내용은 방법: 리본 메뉴에 개발 도구 탭 표시를 참조하십시오.

  3. 컨트롤 그룹에서 추가할 콘텐츠 컨트롤의 아이콘을 클릭합니다.

런타임에 문서 수준 프로젝트에 콘텐츠 컨트롤 추가

런타임에 프로젝트에서 ThisDocument 클래스에 포함된 Controls 속성의 메서드를 사용하여 콘텐츠 컨트롤을 문서에 프로그래밍 방식으로 추가할 수 있습니다. 각 메서드에는 다음 방법으로 콘텐츠 컨트롤을 추가하는 데 사용할 수 있는 세 개의 오버로드가 있습니다.

  • 현재 선택 영역에 컨트롤을 추가합니다.

  • 지정된 범위에 컨트롤을 추가합니다.

  • 문서의 네이티브 콘텐츠 컨트롤을 기반으로 하는 컨트롤을 추가합니다.

동적으로 만들어진 콘텐츠 컨트롤은 문서가 닫힐 때 문서에 유지되지 않습니다. 그러나 네이티브 콘텐츠 컨트롤은 문서에 그대로 유지됩니다. 따라서 다음에 문서가 열릴 때 네이티브 콘텐츠 컨트롤을 기반으로 하는 콘텐츠 컨트롤을 다시 만들 수 있습니다. 자세한 내용은 런타임에 Office 문서에 컨트롤 추가를 참조하십시오.

참고

Word 2010 프로젝트에서 문서에 확인란 콘텐츠 컨트롤을 추가하려면 ContentControl 개체를 만들어야 합니다. 자세한 내용은 콘텐츠 컨트롤을 참조하십시오.

현재 선택 영역에 콘텐츠 컨트롤을 추가하려면

  • 이름이 Add<control class>이고 새 컨트롤의 이름에 대한 단일 매개 변수가 있는 ControlCollection 메서드를 사용합니다. 여기서 control class는 AddRichTextContentControl과 같이 추가할 콘텐츠 컨트롤의 클래스 이름입니다.

    다음 코드 예제에서는 ControlCollection.AddRichTextContentControl(String) 메서드를 사용하여 문서의 시작 부분에 새 RichTextContentControl을 추가합니다. 이 코드를 실행하려면 프로젝트의 ThisDocument 클래스에 해당 코드를 추가하고 ThisDocument_Startup 이벤트 처리기에서 AddRichTextControlAtSelection 메서드를 호출합니다.

    Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddRichTextControlAtSelection()
        Me.Paragraphs(1).Range.InsertParagraphBefore()
        Me.Paragraphs(1).Range.Select()
        richTextControl1 = Me.Controls.AddRichTextContentControl("richTextControl1")
        richTextControl1.PlaceholderText = "Enter your first name"
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;
    
    private void AddRichTextControlAtSelection()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
        this.Paragraphs[1].Range.Select();
    
        richTextControl1 = this.Controls.AddRichTextContentControl("richTextControl1");
        richTextControl1.PlaceholderText = "Enter your first name";
    }
    

지정된 범위에 콘텐츠 컨트롤을 추가하려면

  • 이름이 Add<control class>이고 Microsoft.Office.Interop.Word.Range 매개 변수가 있는 ControlCollection 메서드를 사용합니다. 여기서 control class는 AddRichTextContentControl과 같이 추가할 콘텐츠 컨트롤의 클래스 이름입니다.

    다음 코드 예제에서는 ControlCollection.AddRichTextContentControl(Range, String) 메서드를 사용하여 문서의 시작 부분에 새 RichTextContentControl을 추가합니다. 이 코드를 실행하려면 프로젝트의 ThisDocument 클래스에 해당 코드를 추가하고 ThisDocument_Startup 이벤트 처리기에서 AddRichTextControlAtRange 메서드를 호출합니다.

    Dim richTextControl2 As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddRichTextControlAtRange()
        Me.Paragraphs(1).Range.InsertParagraphBefore()
        richTextControl2 = Me.Controls.AddRichTextContentControl(Me.Paragraphs(1).Range, _
            "richTextControl2")
        richTextControl2.PlaceholderText = "Enter your first name"
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl2;
    
    private void AddRichTextControlAtRange()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
    
        richTextControl2 = this.Controls.AddRichTextContentControl(this.Paragraphs[1].Range,
            "richTextControl2");
        richTextControl2.PlaceholderText = "Enter your first name";
    }
    

네이티브 콘텐츠 컨트롤을 기반으로 하는 콘텐츠 컨트롤을 추가하려면

  • 이름이 Add<control class>이고 Microsoft.Office.Interop.Word.ContentControl 매개 변수가 있는 ControlCollection 메서드를 사용합니다. 여기서 control class는 AddRichTextContentControl과 같이 추가할 콘텐츠 컨트롤의 클래스 이름입니다.

    다음 코드 예제에서는 ControlCollection.AddRichTextContentControl(ContentControl, String) 메서드를 사용하여 문서의 모든 네이티브 서식 있는 텍스트 컨트롤에 대해 새 RichTextContentControl을 만듭니다. 이 코드를 실행하려면 프로젝트의 ThisDocument 클래스에 해당 코드를 추가하고 ThisDocument_Startup 이벤트 처리기에서 CreateRichTextControlsFromNativeControls 메서드를 호출합니다.

    Private richTextControls As New System.Collections.Generic.List _
            (Of Microsoft.Office.Tools.Word.RichTextContentControl)
    
    Private Sub CreateRichTextControlsFromNativeControls()
        If Me.ContentControls.Count <= 0 Then
            Return
        End If
    
        Dim count As Integer = 0
        For Each nativeControl As Word.ContentControl In Me.ContentControls
            If nativeControl.Type = Word.WdContentControlType.wdContentControlRichText Then
                count += 1
                Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _
                    Me.Controls.AddRichTextContentControl(nativeControl, _
                    "VSTORichTextContentControl" + count.ToString())
                richTextControls.Add(tempControl)
            End If
        Next nativeControl
    End Sub
    
    private System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;
    
    private void CreateRichTextControlsFromNativeControls()
    {
        if (this.ContentControls.Count <= 0)
            return;
    
        richTextControls = new System.Collections.Generic.List
            <Microsoft.Office.Tools.Word.RichTextContentControl>();
        int count = 0;
    
        foreach (Word.ContentControl nativeControl in this.ContentControls)
        {
            if (nativeControl.Type ==
                Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
            {
                count++;
                Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
                    this.Controls.AddRichTextContentControl(nativeControl,
                    "VSTORichTextControl" + count.ToString());
                richTextControls.Add(tempControl);
            }
        }
    }
    

런타임에 응용 프로그램 수준 프로젝트에 콘텐츠 컨트롤 추가

런타임에 응용 프로그램 수준 추가 기능을 사용하여 열려 있는 문서에 콘텐츠 컨트롤을 프로그래밍 방식으로 추가할 수 있습니다. 이렇게 하려면 열려 있는 문서를 기반으로 하는 Document 호스트 항목을 생성한 다음 이 호스트 항목에 대한 Controls 속성의 메서드를 사용합니다. 각 메서드에는 다음 방법으로 콘텐츠 컨트롤을 추가하는 데 사용할 수 있는 세 개의 오버로드가 있습니다.

  • 현재 선택 영역에 컨트롤을 추가합니다.

  • 지정된 범위에 컨트롤을 추가합니다.

  • 문서의 네이티브 콘텐츠 컨트롤을 기반으로 하는 컨트롤을 추가합니다.

동적으로 만들어진 콘텐츠 컨트롤은 문서가 닫힐 때 문서에 유지되지 않습니다. 그러나 네이티브 콘텐츠 컨트롤은 문서에 그대로 유지됩니다. 따라서 다음에 문서가 열릴 때 네이티브 콘텐츠 컨트롤을 기반으로 하는 콘텐츠 컨트롤을 다시 만들 수 있습니다. 자세한 내용은 Office 문서에서 동적 컨트롤 유지를 참조하십시오.

응용 프로그램 수준 프로젝트에서 호스트 항목을 생성하는 방법에 대한 자세한 내용은 런타임에 응용 프로그램 수준 추가 기능의 Word 문서 및 Excel 통합 문서 확장을 참조하십시오.

참고

Word 2010 프로젝트에서 문서에 확인란 콘텐츠 컨트롤을 추가하려면 ContentControl 개체를 만들어야 합니다. 자세한 내용은 콘텐츠 컨트롤을 참조하십시오.

현재 선택 영역에 콘텐츠 컨트롤을 추가하려면

  • 이름이 Add<control class>이고 새 컨트롤의 이름에 대한 단일 매개 변수가 있는 ControlCollection 메서드를 사용합니다. 여기서 control class는 AddRichTextContentControl과 같이 추가할 콘텐츠 컨트롤의 클래스 이름입니다.

    다음 코드 예제에서는 ControlCollection.AddRichTextContentControl(String) 메서드를 사용하여 활성 문서의 시작 부분에 새 RichTextContentControl을 추가합니다. 이 코드를 실행하려면 프로젝트의 ThisAddIn 클래스에 해당 코드를 추가하고 ThisAddIn_Startup 이벤트 처리기에서 AddRichTextControlAtSelection 메서드를 호출합니다.

    Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddRichTextControlAtSelection()
        Dim currentDocument As Word.Document = Me.Application.ActiveDocument
        currentDocument.Paragraphs(1).Range.InsertParagraphBefore()
        currentDocument.Paragraphs(1).Range.Select()
    
        ' Use the following line of code in projects that target the .NET Framework 4.
        Dim extendedDocument As Document = Globals.Factory.GetVstoObject(currentDocument)
    
        ' In projects that target the .NET Framework 3.5, use the following line of code.
        ' Dim extendedDocument As Document = currentDocument.GetVstoObject()
    
        richTextControl1 = extendedDocument.Controls.AddRichTextContentControl("richTextControl1")
        richTextControl1.PlaceholderText = "Enter your first name"
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;
    
    private void AddRichTextControlAtSelection()
    {
        Word.Document currentDocument = this.Application.ActiveDocument;
        currentDocument.Paragraphs[1].Range.InsertParagraphBefore();
        currentDocument.Paragraphs[1].Range.Select();
    
        // Use the following line of code in projects that target the .NET Framework 4.
        Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
    
        // In projects that target the .NET Framework 3.5, use the following line of code.
        // Document extendedDocument = currentDocument.GetVstoObject();
    
        richTextControl1 = extendedDocument.Controls.AddRichTextContentControl("richTextControl1");
        richTextControl1.PlaceholderText = "Enter your first name";
    }
    

지정된 범위에 콘텐츠 컨트롤을 추가하려면

  • 이름이 Add<control class>이고 Microsoft.Office.Interop.Word.Range 매개 변수가 있는 ControlCollection 메서드를 사용합니다. 여기서 control class는 AddRichTextContentControl과 같이 추가할 콘텐츠 컨트롤의 클래스 이름입니다.

    다음 코드 예제에서는 ControlCollection.AddRichTextContentControl(Range, String) 메서드를 사용하여 활성 문서의 시작 부분에 새 RichTextContentControl을 추가합니다. 이 코드를 실행하려면 프로젝트의 ThisAddIn 클래스에 해당 코드를 추가하고 ThisAddIn_Startup 이벤트 처리기에서 AddRichTextControlAtRange 메서드를 호출합니다.

    Dim richTextControl2 As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddRichTextControlAtRange()
        Dim currentDocument As Word.Document = Me.Application.ActiveDocument
        currentDocument.Paragraphs(1).Range.InsertParagraphBefore()
    
        ' Use the following line of code in projects that target the .NET Framework 4.
        Dim extendedDocument As Document = Globals.Factory.GetVstoObject(currentDocument)
    
        ' In projects that target the .NET Framework 3.5, use the following line of code.
        ' Dim extendedDocument As Document = currentDocument.GetVstoObject()
    
        richTextControl2 = extendedDocument.Controls.AddRichTextContentControl( _
            extendedDocument.Paragraphs(1).Range, "richTextControl2")
        richTextControl2.PlaceholderText = "Enter your first name"
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl2;
    
    private void AddRichTextControlAtRange()
    {
        Word.Document currentDocument = this.Application.ActiveDocument;
        currentDocument.Paragraphs[1].Range.InsertParagraphBefore();
    
        // Use the following line of code in projects that target the .NET Framework 4.
        Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
    
        // In projects that target the .NET Framework 3.5, use the following line of code.
        // Document extendedDocument = currentDocument.GetVstoObject();
    
        richTextControl2 = extendedDocument.Controls.AddRichTextContentControl(
            currentDocument.Paragraphs[1].Range, "richTextControl2");
        richTextControl2.PlaceholderText = "Enter your first name";
    }
    

네이티브 콘텐츠 컨트롤을 기반으로 하는 콘텐츠 컨트롤을 추가하려면

  • 이름이 Add<control class>이고 Microsoft.Office.Interop.Word.ContentControl 매개 변수가 있는 ControlCollection 메서드를 사용합니다. 여기서 control class는 AddRichTextContentControl과 같이 추가할 콘텐츠 컨트롤의 클래스 이름입니다.

    다음 코드 예제에서는 ControlCollection.AddRichTextContentControl(ContentControl, String) 메서드를 사용하여 문서가 열린 후 문서의 모든 네이티브 서식 있는 텍스트 컨트롤에 대해 새 RichTextContentControl을 만듭니다. 이 코드를 실행하려면 프로젝트의 ThisAddIn 클래스에 이 코드를 추가합니다.

    Private richTextControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.RichTextContentControl)
    
    Private Sub Application_DocumentOpen(ByVal Doc As Microsoft.Office.Interop.Word.Document) _
        Handles Application.DocumentOpen
    
        If Doc.ContentControls.Count > 0 Then
    
            ' Use the following line of code in projects that target the .NET Framework 4.
            Dim extendedDocument As Document = Globals.Factory.GetVstoObject(Doc)
    
            ' In projects that target the .NET Framework 3.5, use the following line of code.
            ' Dim extendedDocument As Document = Doc.GetVstoObject()
    
            Dim count As Integer = 0
            For Each nativeControl As Word.ContentControl In Doc.ContentControls
                If nativeControl.Type = Word.WdContentControlType.wdContentControlRichText Then
                    count += 1
                    Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _
                        extendedDocument.Controls.AddRichTextContentControl(nativeControl, _
                        "VSTORichTextContentControl" + count.ToString())
                    richTextControls.Add(tempControl)
                End If
            Next nativeControl
        End If
    End Sub
    
    private System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;
    
    private void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
    {
        if (Doc.ContentControls.Count > 0)
        {
            // Use the following line of code in projects that target the .NET Framework 4.
            Document extendedDocument = Globals.Factory.GetVstoObject(Doc);
    
            // In projects that target the .NET Framework 3.5, use the following line of code.
            // Document extendedDocument = Doc.GetVstoObject();
    
            richTextControls = new System.Collections.Generic.List
                <Microsoft.Office.Tools.Word.RichTextContentControl>();
            int count = 0;
    
            foreach (Word.ContentControl nativeControl in Doc.ContentControls)
            {
                if (nativeControl.Type ==
                    Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
                {
                    count++;
                    Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
                        extendedDocument.Controls.AddRichTextContentControl(nativeControl,
                        "VSTORichTextControl" + count.ToString());
                    richTextControls.Add(tempControl);
                }
            }
        }
    }
    

    또한 C#의 경우 DocumentOpen 이벤트에 Application_DocumentOpen 이벤트 처리기를 연결해야 합니다.

    this.Application.DocumentOpen +=
        new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
    

참고 항목

개념

확장된 개체를 사용하여 Word 자동화

호스트 항목 및 호스트 컨트롤 개요

런타임에 Office 문서에 컨트롤 추가

호스트 항목 및 호스트 컨트롤의 프로그래밍에 대한 제한 사항

호스트 컨트롤의 도우미 메서드

기타 리소스

응용 프로그램 수준 추가 기능 프로그래밍

문서 수준 사용자 지정 프로그래밍