次の方法で共有


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

更新 : 2008 年 7 月

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

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

構文

'宣言
Public Function AddGroupContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As GroupContentControl
'使用
Dim instance As ControlCollection
Dim contentControl As ContentControl
Dim name As String
Dim returnValue As GroupContentControl

returnValue = instance.AddGroupContentControl(contentControl, _
    name)
public 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);
        }
    }
}

このバージョンは、アプリケーション レベルのアドインに使用されます。このコードを使用するには、プロジェクトの 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 = Me.Application.ActiveDocument.GetVstoObject()
    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 = this.Application.ActiveDocument.GetVstoObject();
    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);
    }
}

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

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

    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    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 = this.Application.ActiveDocument.GetVstoObject();
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlGroup)
    {
        vstoDoc.Controls.AddGroupContentControl(NewContentControl,
            "GroupControl" + NewContentControl.ID);
    }
}

アクセス許可

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

参照

参照

ControlCollection クラス

ControlCollection メンバ

AddGroupContentControl オーバーロード

Microsoft.Office.Tools.Word 名前空間

その他の技術情報

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

ホスト コントロールのヘルパー メソッド

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

履歴の変更

日付

履歴

理由

2008 年 7 月

アプリケーション レベルのアドインのコード例のバージョンを追加

SP1 機能変更