次の方法で共有


ControlCollection.AddGroupContentControl メソッド (ContentControl, String)

文書内のネイティブなコンテンツ コントロールに基づく新しい GroupContentControl を追加します。

名前空間:  Microsoft.Office.Tools.Word
アセンブリ:  Microsoft.Office.Tools.Word (Microsoft.Office.Tools.Word.dll 内)

構文

'宣言
Function AddGroupContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As GroupContentControl
GroupContentControl AddGroupContentControl(
    ContentControl contentControl,
    string name
)

パラメーター

戻り値

型 : Microsoft.Office.Tools.Word.GroupContentControl
文書に追加された GroupContentControl

例外

例外 条件
ArgumentNullException

contentControl は nullnull 参照 (Visual Basic では Nothing) なので、

または

name が nullnull 参照 (Visual Basic では Nothing) であるか、または長さが 0 である場合。

ControlNameAlreadyExistsException

同じ名前のコントロールが既に ControlCollection に存在する場合。

ArgumentException

contentControl は文書パーツ ギャラリーではありません (contentControl の Type プロパティに値 Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlGroup が設定されていません)。

解説

このメソッドを使用して、実行時に文書内に既に存在するネイティブなコンテンツ コントロールに基づく新しい GroupContentControl を追加します。 これは、実行時に GroupContentControl を作成し、この次に文書を開くときに同じコントロールを再作成する場合に便利です。 詳細については、「実行時の Office ドキュメントへのコントロールの追加」を参照してください。

次のコード例は、文書内にあるネイティブなグループ コントロールごとに新しい GroupContentControl を作成します。

このバージョンは、ドキュメント レベルのカスタマイズに使用されます。 このコードを使用するには、プロジェクトの ThisDocument クラスにコードを貼り付け、ThisDocument_Startup メソッドから CreateGroupControlsFromNativeControls メソッドを呼び出します。

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

Private Sub CreateGroupControlsFromNativeControls()
    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.wdContentControlGroup Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.GroupContentControl = _
                Me.Controls.AddGroupContentControl(nativeControl, _
                "VSTOGroupContentControl" + count.ToString())
            groupControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.GroupContentControl> groupControls;

private void CreateGroupControlsFromNativeControls()
{
    if (this.ContentControls.Count <= 0)
        return;

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

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlGroup)
        {
            count++;
            Microsoft.Office.Tools.Word.GroupContentControl tempControl =
                this.Controls.AddGroupContentControl(nativeControl,
                "VSTOGroupControl" + count.ToString());
            groupControls.Add(tempControl);
        }
    }
}

これは、.NET Framework 4 または .NET Framework 4.5 を対象とするアプリケーション レベルのアドイン用のバージョンです。 このコードを使用するには、プロジェクトの ThisAddIn クラスにコードを貼り付け、ThisAddIn_Startup メソッドから CreateGroupControlsFromNativeControls メソッドを呼び出します。

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

Private Sub CreateGroupControlsFromNativeControls()
    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.wdContentControlGroup Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.GroupContentControl = _
                vstoDoc.Controls.AddGroupContentControl(nativeControl, _
                "VSTOGroupContentControl" + count.ToString())
            groupControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.GroupContentControl> groupControls;

private void CreateGroupControlsFromNativeControls()
{
    if (this.Application.ActiveDocument == null)
        return;

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

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

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlGroup)
        {
            count++;
            Microsoft.Office.Tools.Word.GroupContentControl tempControl =
                vstoDoc.Controls.AddGroupContentControl(nativeControl,
                "VSTOGroupControl" + count.ToString());
            groupControls.Add(tempControl);
        }
    }
}

次のコード例は、ユーザーが文書に追加するネイティブなグループ コントロールごとに新しい GroupContentControl を作成します。

このバージョンは、ドキュメント レベルのカスタマイズに使用されます。 このコードを使用するには、プロジェクトの ThisDocument クラスにコードを貼り付けます。 C# では、さらに ThisDocument_GroupContentControlAfterAdd イベント ハンドラーを ThisDocument クラスの ContentControlAfterAdd イベントに追加する必要があります。

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

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlGroup Then
        Me.Controls.AddGroupContentControl(NewContentControl, _
            "GroupControl" + NewContentControl.ID)
    End If
End Sub
void ThisDocument_GroupContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlGroup)
    {
        this.Controls.AddGroupContentControl(NewContentControl,
            "GroupControl" + NewContentControl.ID);
    }
}

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

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

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

.NET Framework セキュリティ

  • 直前の呼び出し元に対する完全な信頼。このメンバーは、部分的に信頼されているコードから使用することはできません。詳細については、「部分信頼コードからのライブラリの使用」を参照してください。

参照

関連項目

ControlCollection インターフェイス

AddGroupContentControl オーバーロード

Microsoft.Office.Tools.Word 名前空間

その他の技術情報

実行時の Office ドキュメントへのコントロールの追加

方法 : Word 文書にコンテンツ コントロールを追加する