次の方法で共有


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 または .NET Framework 4.5 を対象とするアプリケーション レベルのアドイン用のバージョンです。 このコードを使用するには、プロジェクトの 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_RichTextContentControlAfterAdd イベント ハンドラーを ThisDocument クラスの ContentControlAfterAdd イベントに追加する必要があります。

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 または .NET Framework 4.5 を対象とするアプリケーション レベルのアドイン用のバージョンです。 このコードを使用するには、プロジェクトの ThisAddIn クラスにコードを貼り付けます。 また、ActiveDocument_RichTextContentControlAfterAdd イベント ハンドラーをアクティブ文書の ContentControlAfterAdd イベントに追加する必要があります。

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 文書にコンテンツ コントロールを追加する