Share via


ControlCollection.AddRichTextContentControl 메서드 (ContentControl, String)

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

네임스페이스:  Microsoft.Office.Tools.Word
어셈블리:  Microsoft.Office.Tools.Word(Microsoft.Office.Tools.Word.dll)

구문

‘선언
Function AddRichTextContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As RichTextContentControl
RichTextContentControl AddRichTextContentControl(
    ContentControl contentControl,
    string name
)

매개 변수

반환 값

형식: Microsoft.Office.Tools.Word.RichTextContentControl
문서에 추가된 RichTextContentControl입니다.

예외

예외 상황
ArgumentNullException

contentControl는 nullNull 참조(Visual Basic의 경우 Nothing)입니다.

또는

name이 nullNull 참조(Visual Basic의 경우 Nothing)이거나 길이가 0인 경우

ControlNameAlreadyExistsException

이름이 같은 컨트롤이 ControlCollection에 이미 있는 경우

ArgumentException

contentControl이 문서 블록 갤러리가 아닌 경우. 즉, contentControl의 Microsoft.Office.Interop.Word.ContentControl.Type 속성 값이 Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText가 아닌 경우

설명

이 메서드를 사용하여 런타임에 문서의 네이티브 콘텐츠 컨트롤을 기반으로 하는 새 RichTextContentControl을 추가할 수 있습니다. 이 방법은 런타임에 RichTextContentControl을 만들고 다음에 문서를 열 때 동일한 컨트롤을 다시 만들려는 경우에 유용합니다. 자세한 내용은 런타임에 Office 문서에 컨트롤 추가을 참조하십시오.

예제

다음 코드 예제에서는 문서에 있는 모든 네이티브 서식 있는 텍스트 컨트롤에 대해 새 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);
        }
    }
}

이 버전은 .NET Framework 4를 대상으로 하는 응용 프로그램 수준 추가 기능을 위한 것입니다. 이 코드를 사용하려면 프로젝트의 ThisAddIn 클래스에 해당 코드를 붙여넣고 ThisAddIn_Startup 메서드에서 CreateRichTextControlsFromNativeControls 메서드를 호출합니다.

Private richTextControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.RichTextContentControl)

Private Sub CreateRichTextControlsFromNativeControls()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If vstoDoc.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In vstoDoc.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlRichText Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _
                vstoDoc.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.Application.ActiveDocument == null)
        return;

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (vstoDoc.ContentControls.Count <= 0)
        return;

    richTextControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.RichTextContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type ==
            Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
        {
            count++;
            Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
                vstoDoc.Controls.AddRichTextContentControl(nativeControl,
                "VSTORichTextControl" + count.ToString());
            richTextControls.Add(tempControl);
        }
    }
}

다음 코드 예제에서는 사용자가 문서에 추가하는 모든 네이티브 서식 있는 텍스트 컨트롤에 대해 새 RichTextContentControl을 만듭니다.

이 버전은 문서 수준 사용자 지정을 위한 것입니다. 이 코드를 사용하려면 프로젝트의 ThisDocument 클래스에 해당 코드를 붙여넣습니다. 또한 C#의 경우 ThisDocument 클래스의 ContentControlAfterAdd 이벤트에 ThisDocument_RichTextContentControlAfterAdd 이벤트 처리기를 연결해야 합니다.

Private Sub ThisDocument_RichTextContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlRichText Then
        Me.Controls.AddRichTextContentControl(NewContentControl, _
            "RichTextControl" + NewContentControl.ID)
    End If
End Sub
void ThisDocument_RichTextContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlRichText)
    {
        this.Controls.AddRichTextContentControl(NewContentControl,
            "RichTextControl" + NewContentControl.ID);
    }
}

이 버전은 .NET Framework 4를 대상으로 하는 응용 프로그램 수준 추가 기능을 위한 것입니다. 이 코드를 사용하려면 프로젝트의 ThisAddIn 클래스에 해당 코드를 붙여넣습니다. 또한 활성 문서의 ContentControlAfterAdd 이벤트에 ActiveDocument_RichTextContentControlAfterAdd 이벤트 처리기를 연결해야 합니다.

Private Sub ActiveDocument_RichTextContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlRichText Then
        vstoDoc.Controls.AddRichTextContentControl(NewContentControl, _
            "RichTextControl" + NewContentControl.ID)
    End If
End Sub
void ActiveDocument_RichTextContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlRichText)
    {
        vstoDoc.Controls.AddRichTextContentControl(NewContentControl,
            "RichTextControl" + NewContentControl.ID);
    }
}

.NET Framework 보안

  • 직접 실행 호출자의 경우 완전히 신뢰합니다. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다. 자세한 내용은 부분 신뢰 코드에서 라이브러리 사용을 참조하십시오.

참고 항목

참조

ControlCollection 인터페이스

AddRichTextContentControl 오버로드

Microsoft.Office.Tools.Word 네임스페이스

기타 리소스

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

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

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